iOS开源项目学习——SVProgressHUD
发布日期:2022-03-18 08:27:37 浏览次数:30 分类:技术文章

本文共 5025 字,大约阅读时间需要 16 分钟。

SVProgressHUD

代码片段

SVProgressHUD.h中有如下代码:

#if __IPHONE_OS_VERSION_MAX_ALLOWED < 70000#define UI_APPEARANCE_SELECTOR#endif

__IPHONE_OS_VERSION_MAX_ALLOWED用法与意义请参考

UI_APPEARANCE_SELECTOR请参考

在申明方法是见到如下的用法:

+ (void)showWithMaskType:(SVProgressHUDMaskType)maskType __attribute__((deprecated("Use show and setDefaultMaskType: instead.")));

__attribute__ 可以设置函数属性(Function Attribute )、变量属性(Variable Attribute )和类型属性(Type Attribute ),详细的请见:

iOS UIWindow的理解

如下代码:

NSEnumerator *frontToBackWindows = [UIApplication.sharedApplication.windows reverseObjectEnumerator];    for (UIWindow *window in frontToBackWindows) {        BOOL windowOnMainScreen = window.screen == UIScreen.mainScreen;        BOOL windowIsVisible = !window.hidden && window.alpha > 0;        BOOL windowLevelNormal = window.windowLevel == UIWindowLevelNormal;        if(windowOnMainScreen && windowIsVisible && windowLevelNormal) {            [window addSubview:self.overlayView];            break;        }    }

UIWindow在显示的时候会根据UIWindowLevel进行排序的,即Level高的将排在所有Level比他低的层级的前面。下面我们来看UIWindowLevel的定义:

const UIWindowLevel UIWindowLevelNormal;const UIWindowLevel UIWindowLevelAlert;const UIWindowLevel UIWindowLevelStatusBar;typedef CGFloat UIWindowLevel;

它们级别的高低顺序从小到大为Normal < StatusBar < Alert

关于UIWindow的内容请参考:

willMoveToSuperview

SVIndefiniteAnimatedView.m中如下:

- (void)willMoveToSuperview:(UIView*)newSuperview {    if (newSuperview) {        [self layoutAnimatedLayer];    } else {        [_indefiniteAnimatedLayer removeFromSuperlayer];        _indefiniteAnimatedLayer = nil;    }}

willMoveToSuperview的作用如下:

When a view is added to a superview, the system sends willMoveToSuperview: to the view. The parameter is the new superview.

When a view is removed from a superview, the system sends willMoveToSuperview: to the view. The parameter is nil.
You can’t prevent the system from sending willMoveToSuperview: when you remove the view from its superview, but you can check the parameter:

- (void)willMoveToSuperview:(UIView *)newSuperview {if (newSuperview != nil) {  // not a removeFromSuperview situation  }}

从bundle中加载图片

SVProgressHUD使用到的图片,位于SVProgressHUD.bundle中,其加载图片的方式如下:

//资源    NSBundle *bundle = [NSBundle bundleForClass:[SVProgressHUD class]];    NSURL *url = [bundle URLForResource:@"SVProgressHUD" withExtension:@"bundle"];    NSBundle *imageBundle = [NSBundle bundleWithURL:url];    //图片    UIImage* infoImage = [UIImage imageWithContentsOfFile:[imageBundle pathForResource:@"info" ofType:@"png"]];    UIImage* successImage = [UIImage imageWithContentsOfFile:[imageBundle pathForResource:@"success" ofType:@"png"]];    UIImage* errorImage = [UIImage imageWithContentsOfFile:[imageBundle pathForResource:@"error" ofType:@"png"]];

动画

fadeIn

渐入动画,开始出现时,alpha和HUD的transform为:

// Zoom HUD a little to make a nice appear / pop up animation    self.hudView.transform = CGAffineTransformScale(self.hudView.transform, 1.3, 1.3);    // Set initial values to handle iOS 7 (and above) UIToolbar which not answers well to hierarchy opacity change    self.alpha = 0.0f;    self.hudView.alpha = 0.0f;

动画结束的状态为:

__block void (^animationsBlock)(void) = ^{        __strong SVProgressHUD *strongSelf = weakSelf;        if(strongSelf) {            // Shrink HUD to finish pop up animation            strongSelf.hudView.transform = CGAffineTransformScale(strongSelf.hudView.transform, 1/1.3f, 1/1.3f);            strongSelf.alpha = 1.0f;            strongSelf.hudView.alpha = 1.0f;        }    };

相当于有一个弹性的动作。

动画完成后还有执行一段动作,定义为一个completionBlock:

__block void (^completionBlock)(void) = ^{        __strong SVProgressHUD *strongSelf = weakSelf;        if(strongSelf) {            /// Register observer <=> we now have to handle orientation changes etc.            [strongSelf registerNotifications];            // Post notification to inform user            [[NSNotificationCenter defaultCenter] postNotificationName:SVProgressHUDDidAppearNotification                                                                object:strongSelf                                                              userInfo:[strongSelf notificationUserInfo]];        }        // Update accesibilty        UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, nil);        UIAccessibilityPostNotification(UIAccessibilityAnnouncementNotification, status);    };

动画执行的过程为:

//渐入动画,变化alpha和transfrom    if (self.fadeInAnimationDuration > 0) {        // Animate appearance        [UIView animateWithDuration:self.fadeInAnimationDuration                              delay:0                            options:(UIViewAnimationOptions) (UIViewAnimationOptionAllowUserInteraction | UIViewAnimationCurveEaseOut | UIViewAnimationOptionBeginFromCurrentState)                         animations:^{                             animationsBlock();                         } completion:^(BOOL finished) {                             completionBlock();                         }];    } else {        animationsBlock();        completionBlock();    }

fadeOut

fadeOut动画与fadeIn动画类似。

HUD实现

#

转载地址:https://windzen.blog.csdn.net/article/details/51644481 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:iOS事件响应链介绍
下一篇:Beginning iOS Animation Series (Swift 2)

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月06日 23时23分34秒