iOS不得姐项目--pop框架的初次使用
一.pop和Core Animation的区别
1.Core Animation的动画只能添加到layer上
2.pop的动画能添加到任何对象
3.pop的底层并非基于Core Animation,是基于CADisplayLink
4.Core Animation的动画仅仅是表象,并不会真正修改对象的frame/Size等值
5.pop的动画实时修改对象的属性,真正的修改了对象的属性
二.简单结构
三.pop框架简单使用 -- pop不仅可以给layer添加动画,只要是UIView都可以添加动画
1.给view添加动画
2.给layer添加动画
四.项目中的动画效果实现
1.利用pop框架做动画的时候,如果动画关系到了尺寸或者frame,刚开始控件的尺寸或者frame建议不要设置.有时候会出现问题,如下图.由于pop框架做动画是直接修改的属性,所以可以考虑在fromValue中设置尺寸.
2.点击取消按钮的动画,实现思路--直接遍历了控制器中子控件,通过xib添加的控件最早,所以做动画的控件从第2个开始遍历
3.监听按钮的点击 -- 要求是动画依次做完,最后处理对应的事件.
难点:pop框架提供了动画完成的block,自己的目标就是在动画完成后执行相应的事件处理.例如:点击了'发段子'按钮,先做动画,动画完成,跳转到发段子的控制器.
解决方案:抽取方法,将点击取消按钮的动画封装起来,并用block作为参数.点击取消按钮,不需要处理事件,那么block就为nil;如果点击'发段子'之类的按钮,将需要处理的代码用block封装.
1 - (void)cancleWithBlock:(void(^)())btnClickBlock 2 { 3 // 点击了取消之后,动画的过程中就不要处理事件了 4 self.view.userInteractionEnabled = NO; 5 6 for (int i = 2; i < self.view.subviews.count; i++) { 7 8 UIView *subView = self.view.subviews[i]; 9 POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter]; 10 11 anim.toValue = [NSValue valueWithCGPoint:CGPointMake(subView.centerX, subView.centerY + ChaosScreenH)]; 12 anim.beginTime = CACurrentMediaTime() + (i - 2) * ChaosTimeInterval; 13 anim.duration = 0.25; 14 15 [subView pop_addAnimation:anim forKey:nil]; 16 // 监听最后一个view动画的完成 17 if (i == self.view.subviews.count - 1) { 18 [anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) { 19 20 [self dismissViewControllerAnimated:NO completion:nil]; 21 // 判断点击按钮后要执行的block是否为空 22 if (btnClickBlock) { 23 btnClickBlock(); 24 } 25 }]; 26 } 27 } 28 } 29 30 - (IBAction)cancelClick { 31 [self cancleWithBlock:nil]; 32 } 33 34 - (void)buttonClick:(UIButton *)button 35 { 36 [self cancleWithBlock:^{ 37 if (button.tag == 0) { 38 ChaosLog(@"发视频"); 39 } else if (button.tag == 1){ 40 ChaosLog(@"发图片"); 41 } else if (button.tag == 2){ 42 ChaosLog(@"发段子"); 43 } else if (button.tag == 3){ 44 ChaosLog(@"发声音"); 45 } 46 }]; 47 }
五.最后是项目中的代码
1 #import "ChaosPublishViewController.h" 2 #import "ChaosVerticalButton.h" 3 4 #import <POP.h> 5 6 @interface ChaosPublishViewController () 7 8 @end 9 @implementation ChaosPublishViewController 10 11 static CGFloat const ChaosTimeInterval = 0.1; 12 static CGFloat const ChaosAnimSpeed = 15; 13 14 - (void)viewDidLoad { 15 [super viewDidLoad]; 16 17 // 先让所有不能处理事件 18 self.view.userInteractionEnabled = NO; 19 // 数据 20 NSArray *images = @[@"publish-video", @"publish-picture", @"publish-text", @"publish-audio", @"publish-review", @"publish-offline"]; 21 NSArray *titles = @[@"发视频", @"发图片", @"发段子", @"发声音", @"审帖", @"离线下载"]; 22 23 // 设置按钮位置 24 CGFloat buttonW = 72; 25 CGFloat buttonH = buttonW + 30; 26 CGFloat startY = (ChaosScreenH - 2 * buttonH) * 0.5; 27 CGFloat startX = 30; 28 NSInteger maxCols = 3; // 列数 29 CGFloat buttonMargin = (ChaosScreenW - 2 * startX - maxCols * buttonW) / (maxCols - 1); 30 // 添加发布类别 31 for (int i = 0; i < images.count; i++) { 32 ChaosVerticalButton *button = [[ChaosVerticalButton alloc] init]; 33 // button.width = buttonW; // 用了pop后,直接修改了按钮的frame,这里可以先不用设置 34 // button.height = buttonH; // 设置了动画效果反而不好 35 [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal]; 36 [button setTitle:titles[i] forState:UIControlStateNormal]; 37 [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 38 button.titleLabel.font = [UIFont systemFontOfSize:14]; 39 button.tag = i; 40 // 添加点击事件 41 [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; 42 NSInteger btnRow = i / maxCols; // 按钮所在行 43 NSInteger btnCol = i % maxCols; // 按钮所在列 44 CGFloat buttonEndY = startY + btnRow * buttonH; // 按钮最终Y值 45 CGFloat buttonX = startX + btnCol * (buttonMargin + buttonW); // 按钮X值 46 47 [self.view addSubview:button]; 48 49 // 设置动画 50 POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame]; 51 anim.fromValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonEndY - ChaosScreenH, buttonW, buttonH)]; 52 anim.toValue = [NSValue valueWithCGRect:CGRectMake(buttonX, buttonEndY, buttonW, buttonH)]; 53 // anim.springBounciness = 8; 54 anim.springSpeed = ChaosAnimSpeed; 55 anim.beginTime = CACurrentMediaTime() + i * ChaosTimeInterval; 56 [button pop_addAnimation:anim forKey:nil]; 57 } 58 59 // 添加标语 60 [self setupSlogan]; 61 } 62 63 - (void)setupSlogan 64 { 65 // 添加标语 66 UIImageView *sloganImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"app_slogan"]]; 67 sloganImageView.y = -200; 68 [self.view addSubview:sloganImageView]; 69 70 CGFloat centerX = ChaosScreenW * 0.5; 71 CGFloat centerEndY = ChaosScreenH * 0.2; 72 CGFloat centerBeginY = centerEndY - ChaosScreenH; 73 // 设置动画 74 POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter]; 75 anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(centerX, centerBeginY)]; 76 anim.toValue = [NSValue valueWithCGPoint:CGPointMake(centerX, centerEndY)];; 77 // anim.springBounciness = 8; 78 anim.springSpeed = ChaosAnimSpeed; 79 anim.beginTime = CACurrentMediaTime() + 6 * ChaosTimeInterval; 80 [anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) { 81 // 动画完成恢复处理事件的能力 82 self.view.userInteractionEnabled = YES; 83 }]; 84 [sloganImageView pop_addAnimation:anim forKey:nil]; 85 } 86 87 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 88 { 89 [self cancleWithBlock:nil]; 90 } 91 92 - (void)cancleWithBlock:(void(^)())btnClickBlock 93 { 94 // 点击了取消之后,动画的过程中就不要处理事件了 95 self.view.userInteractionEnabled = NO; 96 97 for (int i = 2; i < self.view.subviews.count; i++) { 98 99 UIView *subView = self.view.subviews[i]; 100 POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter]; 101 102 anim.toValue = [NSValue valueWithCGPoint:CGPointMake(subView.centerX, subView.centerY + ChaosScreenH)]; 103 anim.beginTime = CACurrentMediaTime() + (i - 2) * ChaosTimeInterval; 104 anim.duration = 0.25; 105 106 [subView pop_addAnimation:anim forKey:nil]; 107 // 监听最后一个view动画的完成 108 if (i == self.view.subviews.count - 1) { 109 [anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) { 110 111 [self dismissViewControllerAnimated:NO completion:nil]; 112 // 判断点击按钮后要执行的block是否为空 113 if (btnClickBlock) { 114 btnClickBlock(); 115 } 116 }]; 117 } 118 } 119 } 120 121 - (IBAction)cancelClick { 122 [self cancleWithBlock:nil]; 123 } 124 125 - (void)buttonClick:(UIButton *)button 126 { 127 [self cancleWithBlock:^{ 128 if (button.tag == 0) { 129 ChaosLog(@"发视频"); 130 } else if (button.tag == 1){ 131 ChaosLog(@"发图片"); 132 } else if (button.tag == 2){ 133 ChaosLog(@"发段子"); 134 } else if (button.tag == 3){ 135 ChaosLog(@"发声音"); 136 } 137 }]; 138 } 139 140 @end
View Code
转载于:https://www.cnblogs.com/gchlcc/p/5524158.html
iOS不得姐项目--pop框架的初次使用相关推荐
- iOS不得姐项目--封装状态栏指示器(UIWindow实现)
一.头文件 1 #import <UIKit/UIKit.h> 2 3 @interface ChaosStatusBarHUD : NSObject 4 /** 显示成功信息 */ 5 ...
- iOS - iOS ,Mac 开源项目以及库,知识点
https://github.com/trending 目录 UI 下拉刷新 模糊效果 AutoLayout 富文本 图表 表相关与Tabbar 隐藏与显示 HUD与Toast 对话框 其他UI 动画 ...
- 自己总结的 iOS ,Mac 开源项目以及库,知识点------持续更新
自己在 git 上看到一个非常好的总结的东西,但是呢, fork 了几次,就是 fork 不到我的 git 上,干脆复制进去,但是,也是认真去每一个每一个去认真看了,并且也是补充了一些,感觉非常 ...
- iOS好用的第三方框架/插件
一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com/ReactiveCocoa/ReactiveCocoa 2:hud提示框 地址:https://github. ...
- iOS开发-常用第三方开源框架介绍(你了解的ios只是冰山一角)--(转)
图像: 1.图片浏览控件MWPhotoBrowser 实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩放等操作. ...
- (转)iOS开发-常用第三方开源框架介绍(你了解的ios只是冰山一角)
图像: 1.图片浏览控件MWPhotoBrowser 实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩放等操作. ...
- iOS比较常用的第三方框架
[转]http://www.cnblogs.com/wujy/p/4747998.html 把平时看到或项目用到的一些插件进行整理,文章后面分享一些不错的实例,若你有其它的插件欢迎分享,不断的进行更新 ...
- iOS开发-常用第三方开源框架
图片浏览控件MWPhotoBrowser 实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩放等操作. 下载:htt ...
- iOS开发 常用第三方开源框架
iOS开发-常用第三方开源框架介绍(你了解的ios只是冰山一角) 2015-04-05 15:25 2482人阅读 评论(1) 收藏 举报 开源框架 图像: 1.图片浏览控件MWPhotoBrowse ...
最新文章
- pytorch旧版安装
- 通过PEB获取模块基址
- git安装和使用---管理远程仓库
- (chap9 基于HTTP的功能追加协议) WebSocket使用浏览器进行全双工通信
- matlab错误:Variable 'a' cannot be saved to a MAT-file whose version is older than 7.3.
- linux下计算目录文件和,统计Linux 中文件和文件夹/目录的数量(示例代码)
- 如何C#中实现在TreeView查找某一节点
- python开源项目2019_2019年6月Github上最热门的Python开源项目
- IDEA2018部署jeesite3完美运行教程
- Memcached安装及配置
- 这个“达芬奇”不一般!它是美国医生的好帮手
- SkewTransform
- RGB转换为NV12的代码
- 1.0-并发编程-进程和线程简介
- C语言打印结构体的值,怎么才能把结构体里面的二维数组打印出来?
- OpenGL编程指南(第七版)
- 云课堂智慧职教自动签到
- 2020论文阅读:Few-Shot Object Detection with Attention-RPN and Multi-Relation Detector
- Apache网页优化(网页压缩、网页缓存、隐藏版本、配置防盗链)
- 矩阵键盘——按下矩阵按键,数码管显示出位置对应的字符(0123 4567 89Ab CdEF)