iOS控件抖动效果

作者: 爱吃萝卜的小蘑菇 | 来源:发表于2019-04-17 16:55 被阅读0次

    方案一:

    CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
        shake.fromValue = [NSNumber numberWithFloat:-5];
        shake.toValue = [NSNumber numberWithFloat:5];
        shake.duration = 0.1;//执行时间
        shake.autoreverses = YES;//是否重复
        shake.repeatCount = 2;//次数
        [self.schemeOneLable.layer addAnimation:shake forKey:@"shakeAnimation"];
    

    方案二

        CAKeyframeAnimation *shakeAnim = [CAKeyframeAnimation animation];
        shakeAnim.keyPath = @"transform.translation.x";
        shakeAnim.duration = 0.1;
        CGFloat delta = 5;
        shakeAnim.values = @[@0,@(-delta),@(delta),@0];
        shakeAnim.repeatCount = 2;
        [self.schemeTwoLable.layer addAnimation:shakeAnim forKey:nil];
    

    两个方案设置了同样的数据最后的显示结果也不太一样,目前还不清楚原因。

    对应.m文件
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @property (nonatomic,strong) UILabel *schemeOneLable;
    @property (nonatomic,strong) UILabel *schemeTwoLable;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.schemeOneLable = [[UILabel alloc]init];
        self.schemeOneLable.frame = CGRectMake(50, 50, 200, 20);
        self.schemeOneLable.text = @"我想抖~方案一";
        [self.view addSubview:self.schemeOneLable];
        
        self.schemeTwoLable = [[UILabel alloc]init];
        self.schemeTwoLable.frame = CGRectMake(50, 100, 200, 20);
        self.schemeTwoLable.text = @"我想抖~方案二";
        [self.view addSubview:self.schemeTwoLable];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
        shake.fromValue = [NSNumber numberWithFloat:-5];
        shake.toValue = [NSNumber numberWithFloat:5];
        shake.duration = 0.1;//执行时间
        shake.autoreverses = YES;//是否重复
        shake.repeatCount = 2;//次数
        [self.schemeOneLable.layer addAnimation:shake forKey:@"shakeAnimation"];
        
        CAKeyframeAnimation *shakeAnim = [CAKeyframeAnimation animation];
        shakeAnim.keyPath = @"transform.translation.x";
        shakeAnim.duration = 0.1;
        CGFloat delta = 5;
        shakeAnim.values = @[@0,@(-delta),@(delta),@0];
        shakeAnim.repeatCount = 2;
        [self.schemeTwoLable.layer addAnimation:shakeAnim forKey:nil];
    }
    
    @end
    

    相关文章

      网友评论

        本文标题:iOS控件抖动效果

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