iOS 基于UIView抖动 来回移动 缩放 翻转效果

终睿
2023-12-01

1.来回转动 抖动效果

   

 UIImageView  *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

    imageView.center = self.view.center;

    imageView.image = [UIImage imageNamed:@"02.png"]; //自己放一张图片

    [self.view addSubview:imageView];

    

 

CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; //@"transform.rotation.z"此效果是不可以改

    shake.duration = 0.1;            //维持时间

    shake.autoreverses = YES;        //是否自动重复

    shake.repeatCount = MAXFLOAT;   //重复次数 

    shake.removedOnCompletion = NO;  //这个属性默认为 YES,那意味着,在指定的时间段完成后,动画就自动的从层上移除了

     

    shake.fromValue = [NSNumber numberWithFloat:-0.1]; //开始位置

    shake.toValue = [NSNumber numberWithFloat:+0.1];   //结束位置

    [imageView.layer addAnimation:shake forKey:@"imageview"];

  

如果要删除此动画则

[imageView.layer removeAnimationForKey:@"imageview"];  //注意此@"imageview"和之前添加的[imageView.layer addAnimation forKey:@"imageview"]是一样的

或者

[imageView.layer removeAllAnimations];



2.来回移动效果

  

    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

    imageView.center = self.view.center;

    imageView.image = [UIImage imageNamed:@"02"];//自己放一张图片

    [self.view addSubview:imageView];

    

    shake = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"]; //@"transform.translation.x"此效果是可以改成y方向如@"transform.translation.y"

    shake.duration = 1;

    shake.autoreverses = YES;

    shake.repeatCount = MAXFLOAT;

    shake.removedOnCompletion = YES;

    

    shake.fromValue = [NSNumber numberWithFloat:0];

     shake.toValue = [NSNumber numberWithFloat:+100];

    [imageView.layer addAnimation:shake forKey:@"imageview"];



 类似资料: