最近有些空闲时间,整理了下最近做的项目,本文主要介绍了关于iOS自定义控制器转场动画推动的相关内容,分享出来供大家参考学习、下面话不多说了,来一起看看详细的介绍吧。
- (void) addButton { 自我。buttonArr=[NSMutableArray数组); CGFloat保证金=50; CGFloat宽度=(self.view.frame.size.width-margin * 3)/2; CGFloat高度=宽度; CGFloat x=0; CGFloat y=0;//列 NSInteger坳=2; (NSInteger我=0;我& lt;4;我+ +){ x=利润+(我坳)*(利润+宽度); y=利润+ (i/坳)*(利润+高度)+ 150; UIButton *按钮=[UIButton buttonWithType UIButtonTypeCustom):; button.frame=CGRectMake (x, y,宽度、高度); button.layer。cornerRadius=宽* 0.5; 【按钮addTarget:自我行动:@ selector (btnclick:) forControlEvents: UIControlEventTouchUpInside); 按钮。写成backgroundColor=[用户界面颜色colorWithRed: arc4random() % 255/255.0格林:arc4random() % 255/255.0蓝色:arc4random() % 255/255.0α:1.0); 按钮。标签=i + 1; (自我。视图addSubview:按钮); (自我。buttonArr addObject按钮):; } }
添加动画:
- (void) setupButtonAnimation { (自我。buttonArr enumerateObjectsUsingBlock: ^ (UIButton * _Nonnull按钮,了NSUInteger idx, BOOL * _Nonnull停止){//positionAnimation CAKeyframeAnimation * positionAnimation=[CAKeyframeAnimation animationWithKeyPath: @“位置”); positionAnimation。calculationMode=kCAAnimationPaced; positionAnimation。fillMode=kCAFillModeForwards; positionAnimation。repeatCount=MAXFLOAT; positionAnimation。autoreverses=是的; positionAnimation。timingFunction=[CAMediaTimingFunction functionWithName kCAMediaTimingFunctionEaseInEaseOut):; positionAnimation。时间=(idx==self.buttonArr。数- 1)& # 63;4:5 + idx; UIBezierPath * positionPath=[UIBezierPath bezierPathWithOvalInRect: CGRectInset (button.frame button.frame.size.width/2 - 5, button.frame.size.height/2 - 5)]; positionAnimation。路径=positionPath.CGPath; (按钮。层addAnimation: positionAnimation forKey: nil);//scaleXAniamtion CAKeyframeAnimation * scaleXAniamtion=[CAKeyframeAnimation animationWithKeyPath: @“transform.scale.x”); scaleXAniamtion。值=@ (@1.0,@1.1,@1.0); scaleXAniamtion。keyTimes=@ (@0.0, @0.5, @1.0); scaleXAniamtion。repeatCount=MAXFLOAT; scaleXAniamtion。autoreverses=是的; scaleXAniamtion。时间=4 + idx; (按钮。层addAnimation: scaleXAniamtion forKey: nil);//scaleYAniamtion CAKeyframeAnimation * scaleYAnimation=[CAKeyframeAnimation animationWithKeyPath: @“transform.scale.y”); scaleYAnimation。值=@(@1,@1.1和@1.0); scaleYAnimation。keyTimes=@ (@0.0, @0.5, @1.0); scaleYAnimation。autoreverses=是的; scaleYAnimation。repeatCount=是的; scaleYAnimation。时间=4 + idx; (按钮。层addAnimation: scaleYAnimation forKey: nil); }); }
界面搭建好了:
然后想在推的时候实现自定义转场动画首先要遵守一个协议UINavigationControllerDelegate
苹果在UINavigationControllerDelegate中给出了几个协议方法,通过返回类型就可以很清楚地知道各自的具体作用。
//用来自定义转场动画 ——(nullable id)导航控制器:(UINavigationController *)导航控制器animationControllerForOperation: (UINavigationControllerOperation)操作fromViewController: (*) ui fromVC toViewController: (*) ui toVC NS_AVAILABLE_IOS (7 _0);
//为这个动画添加用户交互 ——(nullable id)导航控制器:(UINavigationController *)导航控制器interactionControllerForAnimationController: (id) animationController NS_AVAILABLE_IOS (7 _0);iOS如何自定义控制器转场动画推详解