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

本文共 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年03月18日 16时48分21秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

mysql中用户线程作用,mysql用户线程的建立与用户线程的状态源码解析 2019-04-21
php页面引用公共文件,WeiPHP插件模板中快速引入公共模板文件 2019-04-21
php tracy,admin.php 2021-06-24
php访问父类的所有属性,php – 在父类中使用$this仅在子类中显示父类属性 2021-06-24
oracle比较强大的函数,SQL和ORACLE函数比较 2021-06-24
oracle12c order by,oracle 数据库中order by 的一些高级用法 2021-06-24
oracle8i substr,Oracle中的INSTR,NVL和SUBSTR函数的用法详解 2021-06-24
导出oracle11g的空表,轻松解决oracle11g 空表不能 exp 导出 的问题。 2021-06-24
php把整数拆分成数组,数组拆分处理(整数时的处理),该怎么处理 2021-06-24
oracle numlist,oracle sql str2numlist numtabletype 2021-06-24
php红包平均分配,红包平均分配算法 2021-06-24
linux磁盘的命令是,linux磁盘相关的命令 2021-06-24
linux服务器 缓存,Linux服务器内存使用分析及内存缓存 2021-06-24
linux查进程内存问题,关于linux 查看服务进程内存,cpu,内存占用的一些基础命令... 2021-06-24
linux英文包安装教程视频,Linux源码包安装过程讲解 2021-06-24
linux 关闭rsync服务器,linux下配置rsync服务器和实时同步 2021-06-24
linux初始化TCP服务失败,深入Linux系统追踪TCP初始化 2021-06-24
arch Linux添加源,在Arch Linux系统中使用Archlinuxcn源(清华源)的方法 2021-06-24
私人linux远程连接,Linux远程连接 - osc_5g1gl9wp的个人空间 - OSCHINA - 中文开源技术交流社区... 2021-06-24
windows文件迁移到linux,从Windows到Linux迁移之文件服务器(Samba和AD完美结合) 2021-06-24