关于ios动画

14 篇文章 0 订阅

动画分为两种UIView层动画 CALayer层动画。


UIView层动画分为两种类型:

1.属性动画
2.过渡动画

属性动画之block

    [UIView animateWithDuration:1 animations:^{
        self.AView.alpha = 0.3;
    }];
    [UIView animateWithDuration:3 animations:^{
        self.AView.transform = CGAffineTransformMakeRotation(M_PI_4);
        self.AView.backgroundColor = [UIColor grayColor];
        self.AView.bounds = CGRectMake(0, 0, 100, 100);
    } completion:^(BOOL finished) {
        NSLog(@"结束");
    }];
[UIView animateWithDuration:2 delay:2 options:UIViewAnimationOptionAllowUserInteraction  animations:^{
        self.AView.transform = CGAffineTransformMakeRotation(M_PI_4);
        self.AView.backgroundColor = [UIColor grayColor];
        self.AView.bounds = CGRectMake(0, 0, 100, 100);
    } completion:^(BOOL finished) {
        self.AView.backgroundColor = [UIColor redColor];
    }];
[UIView animateWithDuration:2 delay:2 usingSpringWithDamping:0.1 initialSpringVelocity:2 options:UIViewAnimationOptionAllowUserInteraction animations:^{
          self.AView.transform = CGAffineTransformMakeRotation(M_PI_4);
          self.AView.backgroundColor = [UIColor grayColor];
          self.AView.bounds = CGRectMake(0, 0, 100, 100);
      } completion:^(BOOL finished) {
      }];

过渡动画之 普通翻页效果

 UIView *containerView =self.view.superview;
    [UIViewtransitionWithView:containerView duration:2options:UIViewAnimationOptionTransitionCurlUpanimations:^{
        [self.viewremoveFromSuperview] ;
        [containerView addSubview:XVC.view];
    } completion:^(BOOL finished) {
    }];

CALayer层动画

CAlayer层在做属性动画时并不是所有的属性都可以做动画 只有属性后面带Animatable的才可以做动画。

 CABasicAnimation *basicAnimation = [CABasicAnimationanimationWithKeyPath:@"backgroundColor"];
    basicAnimation.duration =2;
    basicAnimation.fromValue = (__bridgeid)([UIColoryellowColor].CGColor);
    basicAnimation.toValue = (__bridgeid)([UIColorredColor].CGColor);
    [self.AView.layeraddAnimation:basicAnimation forKey:@"BS"];

类似于QQ的弹框的抖动效果

CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimationanimationWithKeyPath:@"position.x"];
    CGFloat x =self.AView.center.x;
    CGFloat leftX = x -10;
    CGFloat rightX = x +10;
    NSNumber *X = [NSNumbernumberWithFloat:x];
    NSNumber *leftx = [NSNumbernumberWithFloat:leftX];
    NSNumber *righty = [NSNumbernumberWithFloat:rightX];
    NSArray *values =@[X,leftx,X,righty,X,leftx,X,righty,X];
    keyFrameAnimation.values = values;
    [self.AView.layeraddAnimation:keyFrameAnimation forKey:@"keyFrame"];

CATransition 转场动画具有方块翻页效果

 CATransition *catransition = [CATransitionanimation];
    //  1获取self.view.superView
    UIView *view =self.view.superview;
    catransition.type =@"cube";
    catransition.duration =3;
    catransition.startProgress =0.3; // 开始动画
    catransition.endProgress =0.8; // 结束动画
    //  2给self.view.superView添加动画
    [view.layeraddAnimation:catransition forKey:@"catransition"];
    //  3移除原来的View,添加新的View
    [self.viewremoveFromSuperview];
    XViewController *XVC = [self.storyboardinstantiateViewControllerWithIdentifier:@"X"];
    [view addSubview:XVC.view];

CAAnimationGroup可融合前面的动画 做出多动画效果

 CAAnimationGroup *group = [CAAnimationGroupanimation];
    CABasicAnimation *basicAnimation = [CABasicAnimationanimationWithKeyPath:@"backgroundColor"];

    basicAnimation.duration =2;
    basicAnimation.fromValue = (__bridgeid)([UIColoryellowColor].CGColor);

    CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimationanimationWithKeyPath:@"position.x"];
    CGFloat x =self.AView.center.x;
    CGFloat leftX = x -10;
    CGFloat rightX = x +10;
    NSNumber *X = [NSNumbernumberWithFloat:x];
    NSNumber *leftx = [NSNumbernumberWithFloat:leftX];
    NSNumber *righty = [NSNumbernumberWithFloat:rightX];

    NSArray *values =@[X  ,leftx,X,righty,X,leftx,X,righty,X];

    keyFrameAnimation.values = values;

    group.duration =2;

    group.animations =@[basicAnimation,keyFrameAnimation];
    [self.AView.layeraddAnimation:group forKey:@"group"];
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值