众所周知, 微信中的摇一摇功能: 搜索人/歌曲/电视,同样在一些其他类app中也有一个摇一摇签到, 摇一摇随机选号等功能,下面以微信摇一摇功能来介绍实现原理.
对于摇一摇功能, 在ios中系统默认为我们提供了摇一摇的功能检测api. ios 中既然已经提供了接口, 我们直接调用就好了.
?
|
1
2
|
#import <quartzcore/quartzcore.h>
#import <audiotoolbox/audiotoolbox.h>
|
实现原理
1. 监听摇一摇方法
?
|
1
2
3
4
5
6
|
// 摇一摇开始
- (void)motionbegan:(uieventsubtype)motion withevent:(nullable uievent *)event ns_available_ios(3_0);
// 摇一摇结束
- (void)motionended:(uieventsubtype)motion withevent:(nullable uievent *)event ns_available_ios(3_0);
// 摇一摇取消
- (void)motioncancelled:(uieventsubtype)motion withevent:(nullable uievent *)event ns_available_ios(3_0);
|
2. 解决摇一摇失效的情况.
ps: 使用 xcode6.x 后创建的项目,仅仅实现第一步监听就可以实现,没有遇到这种问题.
?
|
1
2
3
|
- (bool)canbecomefirstresponder {
return yes;
}
|
3. 摇一摇阶段需要震动及声音.
?
|
1
2
3
4
|
// 摇动开始
- (void)motionbegan:(uieventsubtype)motion withevent:(uievent *)event {
audioservicesplaysystemsound(ksystemsoundid_vibrate);
}
|
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
// 摇动结束
- (void)motionended:(uieventsubtype)motion withevent:(uievent *)event {
if (motion ==uieventsubtypemotionshake ) {
// 1.添加摇动动画
// 见第四点, 推荐第四点的方法二
// 2.设置播放音效
systemsoundid soundid;
nsstring *path = [[nsbundle mainbundle ] pathforresource:@"shake_sound_male" oftype:@"wav"];
audioservicescreatesystemsoundid((__bridge cfurlref)[nsurl fileurlwithpath:path], &soundid);
// 添加摇动声音
audioservicesplaysystemsound (soundid);
// 3.设置震动
audioservicesplaysystemsound(ksystemsoundid_vibrate);
}
}
|
4. 摇一摇阶段需要动画效果.
微信的摇一摇功能是先在视图上放一个摇后要显示的图片:手拿手机的图片, 这个图片就是上下两半拼在一起给人一种一张图片的感觉;当检测到摇一摇 捕捉到晃动事件后,上下两张图片分别上下做一个动画移动(上面的一半往上移,下面的往下移),在completion 里面再移回来.
这里有两种方法:
方法一: 抽出来添加动画效果的方法, 在摇一摇结束方法里添加这个方法.
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
- (void)addanimations {
// 让imgup上下移动
cabasicanimation *translation2 = [cabasicanimation animationwithkeypath:@"position"];
translation2.timingfunction = [camediatimingfunction functionwithname:kcamediatimingfunctioneaseineaseout];
translation2.fromvalue = [nsvalue valuewithcgpoint:cgpointmake(160, 115)];
translation2.tovalue = [nsvalue valuewithcgpoint:cgpointmake(160, 40)];
translation2.duration = 0.5;
translation2.repeatcount = 1;
translation2.autoreverses = yes;
// 让imagdown上下移动
cabasicanimation *translation = [cabasicanimation animationwithkeypath:@"position"];
translation.timingfunction = [camediatimingfunction functionwithname:kcamediatimingfunctioneaseineaseout];
translation.fromvalue = [nsvalue valuewithcgpoint:cgpointmake(160, 345)];
translation.tovalue = [nsvalue valuewithcgpoint:cgpointmake(160, 420)];
translation.duration = 0.5;
translation.repeatcount = 1;
translation.autoreverses = yes;
[self.imgdown.layer addanimation:translation forkey:@"translation"];
[self.imgup.layer addanimation:translation2 forkey:@"translation2"];
}
|
方法二. 在摇一摇开始和结束里添加摇动动画效果及菊花效果
?
|
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
|
/**
* 摇动开始
*/
- (void)motionbegan:(uieventsubtype)motion withevent:(uievent *)event {
nslog(@"开始摇了");
// 菊花显示并开始转动
self.aiload.hidden = no;
[self.aiload startanimating];
audioservicesplaysystemsound(ksystemsoundid_vibrate);
cgfloat offset = self.bgimgview.height * 0.5;
cgfloat duration = 0.4;
[uiview animatewithduration:duration animations:^{
self.imgup.y -= offset;
self.imgdown.y += offset;
}];
}
/**
* 摇动结束
*/
- (void)motionended:(uieventsubtype)motion withevent:(uievent *)event {
nslog(@"摇动结束");
// 不是摇一摇事件则返回
if (motion != uieventsubtypemotionshake) return;
// 1.添加摇动动画
cgfloat offset = self.bgimgview.height * 0.5;
cgfloat duration = 0.4;
[uiview animatewithduration:duration animations:^{
self.imgup.y += offset;
self.imgdown.y -= offset;
}];
// 菊花暂停转动并隐藏
[self.aiload stopanimating];
self.aiload.hidden = yes;
}
|
当然也有使用摇一摇做其他功能的,可以在当结束摇动时,就发送一个网络请求作相关操作即可。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
相关文章
猜你喜欢
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 77
-
2025-05-24 81
-
2025-05-27 76
-
Tomcat Cannot assign requested address: JVM_Bind 非端口占用冲突
2025-05-29 17 -
2025-05-25 103
热门评论


