UI--图标抖动动画

寿伟
2023-12-01

图片左右抖动动画,一张图片,二个按钮,开始和暂停

直接上代码:

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIImageView *imgIOY;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    imageV.backgroundColor = [UIColor yellowColor];
    imageV.image = [UIImage imageNamed:@"lian"];
    imageV.clipsToBounds = YES;
    imageV.layer.cornerRadius = 50;
    [self.view addSubview:imageV];
    self.imgIOY = imageV;
    
    UIButton *btnStart = [UIButton buttonWithType:UIButtonTypeCustom];
    btnStart.backgroundColor = [UIColor purpleColor];
    btnStart.frame = CGRectMake(100, 300, 150, 30);
    [btnStart setTitle:@"开始" forState:UIControlStateNormal];
    [self.view addSubview:btnStart];
    [btnStart addTarget:self action:@selector(btnStart:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btnStop = [UIButton buttonWithType:UIButtonTypeCustom];
    btnStop.backgroundColor = [UIColor purpleColor];
    btnStop.frame = CGRectMake(100, 400, 150, 30);
    [btnStop setTitle:@"结束" forState:UIControlStateNormal];
    [self.view addSubview:btnStop];
    [btnStop addTarget:self action:@selector(btnStop:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)btnStart:(UIButton *)btnStart{
    NSLog(@"动画开始");
    
    CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
    
    
    anim.keyPath = @"transform.rotation";
    
    anim.values = @[@(0), @(-5 * M_PI/180), @(0), @(5 * M_PI/180), @(0)];
    
    anim.repeatCount = MAXFLOAT;
    
    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    
    anim.duration = 0.2;
    
    [self.imgIOY.layer addAnimation:anim forKey:@"dou"];
    
}

- (void)btnStop:(UIButton *)btnStop{
    NSLog(@"动画结束");
    [self.imgIOY.layer removeAnimationForKey:@"dou"];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

OK...

 

转载于:https://www.cnblogs.com/LzwBlog/p/5794481.html

 类似资料: