Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gurdeep0602/bafb86ff63b993716b4e5c24d1b4024d to your computer and use it in GitHub Desktop.
Save Gurdeep0602/bafb86ff63b993716b4e5c24d1b4024d to your computer and use it in GitHub Desktop.
AppSwitchScrollStopper
// UIWindow+AppSwitchScrollStopper.h
// Created by Tim Johnsen on 3/27/16.
#import <UIKit/UIKit.h>
@interface UIWindow (AppSwitchScrollStopper)
/// Call this early on in your app's lifecycle to avoid
/// scroll-related flashing when your app resumes from the background
- (void)installAppSwitchScrollStopper;
@end
// UIWindow+AppSwitchScrollStopper.m
// Created by Tim Johnsen on 3/27/16.
#import "UIWindow+AppSwitchScrollStopper.h"
@interface UIView (AppSwitchScrollStopper)
- (void)recursiveStopScrolling;
@end
@implementation UIView (AppSwitchScrollStopper)
- (void)recursiveStopScrolling
{
if ([self isKindOfClass:[UIScrollView class]]) {
UIScrollView *scrollView = (UIScrollView *)self;
// Stop the scrolling (http://stackoverflow.com/a/3421387/3943258)
[scrollView setContentOffset:scrollView.contentOffset animated:NO];
// Stop the scroll indicators from being captured
if (scrollView.showsHorizontalScrollIndicator) {
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = YES;
}
if (scrollView.showsVerticalScrollIndicator) {
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = YES;
}
}
for (UIView *view in self.subviews) {
[view recursiveStopScrolling];
}
}
@end
@implementation UIWindow (AppSwitchScrollStopper)
+ (void)load
{
void (^stopScrollingBlock)(NSNotification *_Nonnull notification) = ^(NSNotification *_Nonnull notification) {
for (UIWindow *window in [self windowsToStopScrolling]) {
[window recursiveStopScrolling];
}
};
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:stopScrollingBlock];
// Works in iOS extensions too!
if (&NSExtensionHostDidEnterBackgroundNotification) {
[[NSNotificationCenter defaultCenter] addObserverForName:NSExtensionHostDidEnterBackgroundNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:stopScrollingBlock];
}
}
+ (NSMutableSet<UIWindow *> *)windowsToStopScrolling
{
static NSMutableSet<UIWindow *> *windowsToStopScrolling = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
windowsToStopScrolling = [NSMutableSet new];
});
return windowsToStopScrolling;
}
- (void)installAppSwitchScrollStopper
{
[[[self class] windowsToStopScrolling] addObject:self];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment