iOS摇一摇

前言

简单实现摇一摇功能

环境 & 工具

macOS Sierra 10.12.3
Xcode 8.2.1
cartool

理论

  1. 设置引用允许摇一摇
  2. 设置第一响应者
  3. 在触发摇一摇后的事件中添加代码即可

实践

  先从iTunes中下载一个有摇一摇功能的App,拆包找资源,发现图片资源放在Assets.car中了Google后,发现这个cartool可以,原理是使用了CoreUI.framework中私有方法,因为不管怎么加密,最终的工程中要引用正常的图片,那必定有解密的方法,git下源代码,编译后得到二进制的可执行文件
  使用如下指令导出Assets.car中的图片资源

1
2
# 桌面上新建了Assets目录
» ~/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 {
// 绕着垂直于手机的z轴旋转,呈现的就是摇一摇动画
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

参考

Charles使用二三事 FFmpeg学习笔记01(环境搭建)
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×