前言
简单实现摇一摇功能
环境 & 工具
macOS Sierra 10.12.3
Xcode 8.2.1
cartool
理论
- 设置引用允许摇一摇
- 设置第一响应者
- 在触发摇一摇后的事件中添加代码即可
实践
先从iTunes中下载一个有摇一摇功能的App,拆包找资源,发现图片资源放在Assets.car中了Google后,发现这个cartool可以,原理是使用了CoreUI.framework中私有方法,因为不管怎么加密,最终的工程中要引用正常的图片,那必定有解密的方法,git下源代码,编译后得到二进制的可执行文件
使用如下指令导出Assets.car中的图片资源
1 2
| » ~/Desktop ./cartool Assets.car Assets
|
找到我需要的图片资源
新建工程,编写相应的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [UIApplication sharedApplication].applicationSupportsShakeToEdit = true; [self becomeFirstResponder]; _shakeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shakeImage"]]; _shakeImageView.frame = self.view.bounds; _shakeImageView.contentMode = UIViewContentModeScaleAspectFit; [self.view addSubview:_shakeImageView]; [self performSelector:@selector(motionBegan:withEvent:) withObject:nil]; }
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { NSLog(@"开始摇动"); [self shakeSender]; }
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event { NSLog(@"取消摇动"); }
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { if (event.subtype == UIEventSubtypeMotionShake) { NSLog(@"摇动结束"); } }
- (void)shakeSender { CABasicAnimation* shake = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; shake.fromValue = [NSNumber numberWithFloat:-0.2]; shake.toValue = [NSNumber numberWithFloat:+0.2]; shake.duration = 0.2; shake.autoreverses = YES; shake.repeatCount = 99; [_shakeImageView.layer addAnimation:shake forKey:@"imageView"]; _shakeImageView.alpha = 1.0; [UIView animateWithDuration:2.0 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{} completion:nil]; }
|
效果图

工程Demo
参考