设置允许摇一摇功能
[UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;
成为第一响应者
[self becomeFirstResponder];
协议方法
//开始摇动- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
return;
}
//取消摇动- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
return;
}
//摇动结束- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (event.subtype == UIEventSubtypeMotionShake) {
// 判断是否是结束
}
return;
}
代码片
#import "ViewController.h"
@interface ViewController ()
{
UIImageView *moveImageView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
moveImageView = [[UIImageView alloc] init];
moveImageView.bounds = CGRectMake(0, 0, 177, 177);
moveImageView.center = self.view.center;
moveImageView.image = [UIImage imageNamed:@"move"];
[self.view addSubview:moveImageView];
// 设置允许摇一摇
[UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;
// 成为第一响应者
[self becomeFirstResponder];}
#pragma mark - 协议方法
//开始摇动
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
NSLog(@"开始摇动");
CAKeyframeAnimation *KFAnimtion = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//动画完成式是否移除
KFAnimtion.removedOnCompletion = NO;
//动画执行后的状态
KFAnimtion.fillMode = kCAFillModeForwards;
//运动的所有点 第一个是开始位置 目标位置
KFAnimtion.values = [NSArray arrayWithObjects:
[NSValue valueWithCGPoint:CGPointMake( self.view.center.x, self.view.center.y)],
[NSValue valueWithCGPoint:CGPointMake(self.view.center.x-50, self.view.center.y)],
[NSValue valueWithCGPoint:CGPointMake(self.view.center.x+50, self.view.center.y)],
[NSValue valueWithCGPoint:CGPointMake(self.view.center.x-50, self.view.center.y)],
[NSValue valueWithCGPoint:CGPointMake(self.view.center.x+50, self.view.center.y)],
[NSValue valueWithCGPoint:CGPointMake(self.view.center.x-50, self.view.center.y)],
[NSValue valueWithCGPoint:CGPointMake(self.view.center.x+50, self.view.center.y)],
[NSValue valueWithCGPoint:CGPointMake(self.view.center.x-50, self.view.center.y)],
[NSValue valueWithCGPoint:CGPointMake(self.view.center.x+50, self.view.center.y)],
[NSValue valueWithCGPoint:CGPointMake(self.view.center.x, self.view.center.y)], nil];
//这里面不是时间 而是对应上面动画的位置 时间指的是到达某一个点的时间点(总时间*比例)
KFAnimtion.keyTimes = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:.1],
[NSNumber numberWithFloat:.2],
[NSNumber numberWithFloat:.3],
[NSNumber numberWithFloat:.4],
[NSNumber numberWithFloat:.5],
[NSNumber numberWithFloat:.6],
[NSNumber numberWithFloat:.7],
[NSNumber numberWithFloat:.8],
[NSNumber numberWithFloat:.9],
[NSNumber numberWithFloat:1],nil];
//总时间
KFAnimtion.duration = .5;
[moveImageView.layer addAnimation:KFAnimtion forKey:@"key"]; return;
}
//取消摇动
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
NSLog(@"取消摇动");
return;
}
//摇动结束
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.subtype == UIEventSubtypeMotionShake) {
// 判断是否是摇动结束
NSLog(@"摇动结束");
//[moveImageView.layer removeAllAnimations];
}
return;
}
@end