IOS之 基本动画原理

   IOS动画分为属性动画和过渡动画。ios4.0之前

属性动画

内容和设置主要放在方括号中既:如下

[UIView beginAnimations:@"move" context:@"aa"];

中间部分设置动画内容和属性

[UIView commitAnimations];

详见代码如下

    [UIView beginAnimations:@"move" context:@"aa"];
    [UIView setAnimationDuration:1];//设置动画时间
    CGPoint point = self.View1.center;//
    [UIView setAnimationDelegate:self];//设置代理
    [UIView setAnimationWillStartSelector:@selector(startAnimation:)];//动画开始之前
    [UIView setAnimationDidStopSelector:@selector(stopAnition:)];//动画结束之后
    
//    [UIView setAnimationRepeatAutoreverses:YES];
    self.View1.center = CGPointMake(point.x+1, point.y);
//    self.View1.backgroundColor = [UIColor orangeColor];
    [UIView commitAnimations];

一下是能够设置属性动画的属性:

The following properties of the UIView class are animatable:

  @property frame
  @property bounds
  @property center
  @property transform
  @property alpha
  @property backgroundColor
  @property contentStretch

过渡动画:

    [UIView beginAnimations:@"move" context:@"text"];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.View3 cache:YES];

    
    [UIView commitAnimations];

能够设置的属性:

typedef NS_ENUM(NSInteger, UIViewAnimationTransition) {
    UIViewAnimationTransitionNone,
    UIViewAnimationTransitionFlipFromLeft,
    UIViewAnimationTransitionFlipFromRight,
    UIViewAnimationTransitionCurlUp,
    UIViewAnimationTransitionCurlDown,
};


4.0之后修改为调用block

Animating Views with Block Objects

//属性动画

+ animateWithDuration:delay:options:animations:completion:
+ animateWithDuration:animations:completion:
+ animateWithDuration:animations:

//过渡动画

+ transitionWithView:duration:options:animations:completion:
+ transitionFromView:toView:duration:options:completion:

//关键帧动画
+ animateKeyframesWithDuration:delay:options:animations:completion:
+ addKeyframeWithRelativeStartTime:relativeDuration:animations:

//系统动画
+ performSystemAnimation:onViews:options:animations:completion:

//未知,还为测试
+ animateWithDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:
+ performWithoutAnimation:



CAAnimation动画

代码如下:具体过程。

    //CABasicAnimation 动画
    
    CABasicAnimation* baseAnimation = [CABasicAnimation animationWithKeyPath:@"center"];
    baseAnimation.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 0, 0)];
    
    baseAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(400, 100)] ;
    baseAnimation.duration = 3;
    [self.View2.layer addAnimation:baseAnimation forKey:@"baseAnimation"];
    // CAKeyframeAnimation 动画
    CAKeyframeAnimation* keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    keyAnimation.values =[NSArray arrayWithObjects:
                          [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x+5, self.View2.center.y)],
                          [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x, self.View2.center.y)],
                          [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x-5, self.View2.center.y)],
                          [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x, self.View2.center.y)],
                          [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x+5, self.View2.center.y)],
                          [NSValue valueWithCGPoint:CGPointMake(self.View2.center.x, self.View2.center.y)],
                          nil];
    keyAnimation.keyTimes = @[[NSNumber numberWithFloat:.1],
                              [NSNumber numberWithFloat:.2],
                              [NSNumber numberWithFloat:.3],
                              [NSNumber numberWithFloat:.4],
                              [NSNumber numberWithFloat:.5],
                              [NSNumber numberWithFloat:1]];//设置时间 ,百分比
    keyAnimation.calculationMode = kCAAnimationDiscrete;
    [self.View2.layer addAnimation:keyAnimation forKey:@"position"];
    //CATransition 动画
     CATransition* transiton = [CATransition animation];
    transiton.startProgress = 0;
    transiton.endProgress = 1.0;
    transiton.duration = 3;
    transiton.type = kCATransitionReveal;//根据不同值显示不同动画
    transiton.subtype = kCATransitionFromRight;
    [self.view.layer addAnimation:transiton forKey:@"transtion"];



        keyFrameAnimation.fillMode = kCAFillModeForwards;//确定动画 执行完毕之后模式(删除or保留)
        keyFrameAnimation.removedOnCompletion = NO;//删除动画设置为no

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值