From 70d723c381e9b3c6336c52d477cf7459c9d8870f Mon Sep 17 00:00:00 2001 From: Tobias Tiemerding Date: Thu, 27 Aug 2015 15:03:13 +0200 Subject: [PATCH] Fix weak / strong references in blocks --- SVProgressHUD/SVProgressHUD.m | 91 ++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 40 deletions(-) diff --git a/SVProgressHUD/SVProgressHUD.m b/SVProgressHUD/SVProgressHUD.m index 0dd9e997..e8e13a8d 100644 --- a/SVProgressHUD/SVProgressHUD.m +++ b/SVProgressHUD/SVProgressHUD.m @@ -777,16 +777,20 @@ - (void)showProgress:(float)progress status:(NSString*)string{ self.hudView.alpha = 0; } + __weak SVProgressHUD *weakSelf = self; [UIView animateWithDuration:0.15 delay:0 options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState animations:^{ - self.hudView.transform = CGAffineTransformScale(self.hudView.transform, 1/1.3f, 1/1.3f); - - if(self.isClear){ // handle iOS 7 and 8 UIToolbar which not answers well to hierarchy opacity change - self.hudView.alpha = 1; - } else{ - self.alpha = 1; + __strong SVProgressHUD *strongSelf = weakSelf; + if(strongSelf){ + strongSelf.hudView.transform = CGAffineTransformScale(strongSelf.hudView.transform, 1/1.3f, 1/1.3f); + + if(strongSelf.isClear){ // handle iOS 7 and 8 UIToolbar which not answers well to hierarchy opacity change + strongSelf.hudView.alpha = 1; + } else{ + strongSelf.alpha = 1; + } } } completion:^(BOOL finished){ @@ -862,49 +866,56 @@ - (void)dismiss{ userInfo:userInfo]; self.activityCount = 0; + __weak SVProgressHUD *weakSelf = self; [UIView animateWithDuration:0.15 delay:0 options:(UIViewAnimationOptions) (UIViewAnimationCurveEaseIn | UIViewAnimationOptionAllowUserInteraction) animations:^{ - self.hudView.transform = CGAffineTransformScale(self.hudView.transform, 0.8f, 0.8f); - if(self.isClear){ // handle iOS 7 UIToolbar not answer well to hierarchy opacity change - self.hudView.alpha = 0.0f; - } else{ - self.alpha = 0.0f; + __strong SVProgressHUD *strongSelf = weakSelf; + if(strongSelf){ + strongSelf.hudView.transform = CGAffineTransformScale(self.hudView.transform, 0.8f, 0.8f); + if(strongSelf.isClear){ // handle iOS 7 UIToolbar not answer well to hierarchy opacity change + strongSelf.hudView.alpha = 0.0f; + } else{ + strongSelf.alpha = 0.0f; + } } } completion:^(BOOL finished){ - if(self.alpha == 0.0f || self.hudView.alpha == 0.0f){ - self.alpha = 0.0f; - self.hudView.alpha = 0.0f; - - [[NSNotificationCenter defaultCenter] removeObserver:self]; - [self cancelRingLayerAnimation]; - [_hudView removeFromSuperview]; - _hudView = nil; - - [_overlayView removeFromSuperview]; - _overlayView = nil; - - [_indefiniteAnimatedView removeFromSuperview]; - _indefiniteAnimatedView = nil; - - UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nil); - - [[NSNotificationCenter defaultCenter] postNotificationName:SVProgressHUDDidDisappearNotification - object:nil - userInfo:userInfo]; - - // Tell the rootViewController to update the StatusBar appearance + __strong SVProgressHUD *strongSelf = weakSelf; + if(strongSelf){ + if(strongSelf.alpha == 0.0f || strongSelf.hudView.alpha == 0.0f){ + strongSelf.alpha = 0.0f; + strongSelf.hudView.alpha = 0.0f; + + [[NSNotificationCenter defaultCenter] removeObserver:strongSelf]; + [strongSelf cancelRingLayerAnimation]; + [_hudView removeFromSuperview]; + _hudView = nil; + + [_overlayView removeFromSuperview]; + _overlayView = nil; + + [_indefiniteAnimatedView removeFromSuperview]; + _indefiniteAnimatedView = nil; + + UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nil); + + [[NSNotificationCenter defaultCenter] postNotificationName:SVProgressHUDDidDisappearNotification + object:nil + userInfo:userInfo]; + + // Tell the rootViewController to update the StatusBar appearance #if !defined(SV_APP_EXTENSIONS) - UIViewController *rootController = [[UIApplication sharedApplication] keyWindow].rootViewController; - if([rootController respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]){ - [rootController setNeedsStatusBarAppearanceUpdate]; - } + UIViewController *rootController = [[UIApplication sharedApplication] keyWindow].rootViewController; + if([rootController respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]){ + [rootController setNeedsStatusBarAppearanceUpdate]; + } #endif - // uncomment to make sure UIWindow is gone from app.windows - //NSLog(@"%@", [UIApplication sharedApplication].windows); - //NSLog(@"keyWindow = %@", [UIApplication sharedApplication].keyWindow); + // uncomment to make sure UIWindow is gone from app.windows + //NSLog(@"%@", [UIApplication sharedApplication].windows); + //NSLog(@"keyWindow = %@", [UIApplication sharedApplication].keyWindow); + } } }]; }