Determine if two controls overlap on the same window
Write classification to UIView-(BOOL) Intersectwithview: (UIView *) view;-(BOOL) Intersectwithview: (UIView *) view{ UIWindow *window = [UIApplication Sharedapplication].keywindow; CGRect selfrect = [self convertRect:self.bounds toview:window]; CGRect viewrect = [view ConvertRect:view.bounds Toview:window]; Return Cgrectintersectsrect (Selfrect, viewrect);}
Add a higher-level uiwindow to let ScrollView scroll to the top when you click the status bar
. h file #import <Foundation/Foundation.h> @interface jgtopwindow:nsobject+ (void) show; @end//.m file #import " JGTopWindow.h "@implementation jgtopwindowstatic uiwindow *window_;+ (void) show{Dispatch_after (Dispatch_time ( Dispatch_time_now, (int64_t) (0.25 * nsec_per_sec)), Dispatch_get_main_queue (), ^{window_ = [[UIWindow alloc] init] ; Window_.frame = [UIApplication sharedapplication].statusbarframe; The window is displayed by default black Window_.backgroundcolor = [Uicolor Clearcolor]; Uiwindowlevelalert > Uiwindowlevelstatusbar > Uiwindowlevelnormal//window default level is Uiwindowlevelnormal, lowest level, placed on the bottom layer , click Gesture is intercepted, can not be heard window_.windowlevel = Uiwindowlevelalert; The default is hidden Window_.hidden = NO; [Window_ Addgesturerecognizer:[[uitapgesturerecognizer alloc] initwithtarget:self action: @selector (Topwindowclick) ]]; });} + (void) topwindowclick{//main window UIWindow *window = [UIApplication Sharedapplication].keywindow; Find all ScrollView in the main window [sElf Findscrollviewsinview:window];} /** * Find all ScrollView */+ (void) Findscrollviewsinview in view: (UIView *) view{//Use recursion to find all child controls for (UIView *subview in View.subviews) {[Self findscrollviewsinview:subview]; } if (![ View Iskindofclass:[uiscrollview class]]) return; Determine if there is overlap with window if (![ View Intersectwithview:[uiapplication Sharedapplication].keywindow]) return; CGRect windowrect = [UIApplication sharedapplication].keywindow.bounds; CGRect viewrect = [view ConvertRect:view.bounds Toview:nil]; does not overlap with window//if (! Cgrectintersectsrect (Windowrect, Viewrect)) return; If it is scrollView uiscrollview *scrollview = (Uiscrollview *) view; Modify offset Cgpoint offset = scrollview.contentoffset; Offset.y =-ScrollView.contentInset.top; [ScrollView Setcontentoffset:offset Animated:yes]; [ScrollView scrollrecttovisible:cgrectmake (0, 0, 1, 1) animated:yes];} @end
Determine if two controls overlap on the same window