美文网首页
iOS控件动画分享

iOS控件动画分享

作者: HelloRyan | 来源:发表于2016-08-09 21:06 被阅读103次

    1.水平晃动(登录失败,请求错误)

    CAKeyframeAnimation *keyframeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
    keyframeAnimation.values = @[@-5, @0, @5, @0];
    keyframeAnimation.repeatCount = 3;
    keyframeAnimation.duration = 0.1;
    [SomeView.layer addAnimation:keyframeAnimation forKey:nil];
    

    2.抖动效果(删除控件)

    #define angelToRandian(x)  ((x)/180.0 * M_PI)
    
    CAKeyframeAnimation *keyframeAnimation = [CAKeyframeAnimation animation];
    keyframeAnimation.keyPath = @"transform.rotation";
    keyframeAnimation.values = @[@(angelToRandian(-1.5)),@(angelToRandian(1.5)),@(angelToRandian(-1.5))]; 
    keyframeAnimation.repeatCount = MAXFLOAT;
    keyframeAnimation.duration = 0.2;
    [SomeView.layer addAnimation:keyframeAnimation forKey:nil];`
    

    3.弹出、缩小隐藏效果(自定义一个视图,在设置其隐藏属时触发动画)

    // 自定义初始化方法
    - (instancetype)init {   
       if (self = [super init]) {              
          // 初始化视图       
          [self initViews];    
       }              
          return self;
    }
    // 初始化视图
    - (void)initViews {   
        // 这里创建了三个button
        _qqLoginBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, 80, 80)];   
        [_qqLoginBtn setImage:[UIImage imageNamed:@"Icon_qq.png"] forState:UIControlStateNormal];    
        [self addSubview:_qqLoginBtn]; 
        _weChatLoginBtn = [[UIButton alloc] initWithFrame:CGRectMake(110, 10, 80, 80)];   
        [_weChatLoginBtn setImage:[UIImage imageNamed:@"Icon_weChat.png"] forState:UIControlStateNormal];    
        [self addSubview:_weChatLoginBtn];
        _sinaLoginBtn = [[UIButton alloc] initWithFrame:CGRectMake(205, 10, 80, 80)];    
        [_sinaLoginBtn setImage:[UIImage imageNamed:@"Icon_sina.png"] forState:UIControlStateNormal];    
        [self addSubview:_sinaLoginBtn];   
    }
    // 设置出现、隐藏动画
    - (void)setHidden:(BOOL)hidden {       
      if (hidden) {        
         // 隐藏动画       
         [UIView animateWithDuration:.35 delay:0 usingSpringWithDamping:1 initialSpringVelocity:30   options:UIViewAnimationOptionCurveEaseInOut   animations:^{                       
            _qqLoginBtn.transform = CGAffineTransformMakeScale(0.01, 0.01);            
            _weChatLoginBtn.transform = CGAffineTransformMakeScale(0.01, 0.01);                                    
            _sinaLoginBtn.transform = CGAffineTransformMakeScale(0.01, 0.01);      
         } completion:^(BOOL finished) {         
            [super setHidden:hidden];      
         }];  
      }else {        
          // 显示动画      
          [super setHidden:hidden];             
          [UIView animateWithDuration:.35 delay:0 usingSpringWithDamping:0.5      initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseInOut animations:^{                     
             _qqLoginBtn.transform = CGAffineTransformMakeScale(1, 1);           
             _weChatLoginBtn.transform = CGAffineTransformMakeScale(1, 1);          
             _sinaLoginBtn.transform = CGAffineTransformMakeScale(1, 1);     
          } completion:nil];   
       }
    }

    相关文章

      网友评论

          本文标题:iOS控件动画分享

          本文链接:https://www.haomeiwen.com/subject/kumdsttx.html