标签:
直接上代码:
// // ViewController.m // 密码错误--抖动动画 // // Created by 刘志武 on 16/8/6. // Copyright © 2016年 zhiwuLiu. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 300, 200, 30)]; label.backgroundColor = [UIColor purpleColor]; label.tag = 1000; [self.view addSubview:label]; } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ UILabel *label = [self.view viewWithTag:1000]; [self shakeAnimationForView:label]; } #pragma mark 抖动动画 - (void)shakeAnimationForView:(UIView *) view { // 获取到当前的View CALayer *viewLayer = view.layer; // 获取当前View的位置 CGPoint position = viewLayer.position; // 移动的两个终点位置 CGPoint x = CGPointMake(position.x + 5, position.y); CGPoint y = CGPointMake(position.x - 5, position.y); // 设置动画 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; // 设置运动形式 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; // 设置开始位置 [animation setFromValue:[NSValue valueWithCGPoint:x]]; // 设置结束位置 [animation setToValue:[NSValue valueWithCGPoint:y]]; // 设置自动反转 [animation setAutoreverses:YES]; // 设置时间 [animation setDuration:.06]; // 设置次数 [animation setRepeatCount:3]; // 添加上动画 [viewLayer addAnimation:animation forKey:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
标签:
原文地址:http://www.cnblogs.com/LzwBlog/p/5744382.html