iOS ---进阶之摇一摇
1、摇一摇的原理分析
1)在摇动手机时会产生一个动画,界面的图片会在中间分开分别进行向上、向下的位置移动。
分析:此过程就是在主屏幕上设置两个imageView,在开始摇动的方法中对这两个imageView进行位置移动,界面的层次结构如下图:
2)在界面进行动画操作的同时播放音频
分析:在执行动画的方法中添加播放音频的代码
3)在结束晃动时做出相应的操作:发送随机数据请求、页面的跳转等等
2、VC.m文件的代码如下:
#import "ViewController.h"
#import "NextViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#define SCREEN_WIDTH self.view.bounds.size.width
#define SCREEN_HEIGHT self.view.bounds.size.height
@interface ViewController ()
@property (strong,nonatomic)UIImageView * topImage;
@property (strong,nonatomic)UIImageView * bottomImage;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
[UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;
[self setupImageView]; }
/**
* 播放MP3
*/
- (void)playMp3
{
NSString * mp3Path = [[NSBundle mainBundle]pathForResource:@"glass.wav" ofType:nil]; NSURL * soundUrl = [NSURL fileURLWithPath:mp3Path];
SystemSoundID soundId;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(soundUrl), &soundId);
AudioServicesPlaySystemSound(soundId); }
- (void)setupImageView
{
self.topImage = [[UIImageView alloc]initWithFrame:CGRectMake(, , SCREEN_WIDTH, SCREEN_HEIGHT/)];
self.topImage.image = [UIImage imageNamed:@"Shake_01"];
[self.view addSubview:self.topImage]; self.bottomImage = [[UIImageView alloc]initWithFrame:CGRectMake(, SCREEN_HEIGHT/, SCREEN_WIDTH, SCREEN_HEIGHT/)];
self.bottomImage.image = [UIImage imageNamed:@"Shake_02"];
[self.view addSubview:self.bottomImage]; }
/**
* 开始摇动
*/
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
[UIView animateWithDuration:1.0 animations:^{ [self playMp3];
self.topImage.transform = CGAffineTransformMakeTranslation(, -);
self.bottomImage.transform = CGAffineTransformMakeTranslation(, );
}]; }
/**
* 结束取消
*/
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
[UIView animateWithDuration:1.0 animations:^{ self.topImage.transform = CGAffineTransformIdentity;
self.bottomImage.transform = CGAffineTransformIdentity; } completion:^(BOOL finished) { [self.navigationController pushViewController:[[NextViewController alloc]init] animated:YES];
}];
}
/**
* 结束摇动
*/
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
[UIView animateWithDuration:1.0 animations:^{ self.topImage.transform = CGAffineTransformIdentity;
self.bottomImage.transform = CGAffineTransformIdentity; } completion:^(BOOL finished) { [self.navigationController pushViewController:[[NextViewController alloc]init] animated:YES];
}]; }
@end
3、测试的时候最好在真机上测试。
demo下载地址:https://github.com/fengzhihao123/FZHShake
iOS ---进阶之摇一摇的更多相关文章
- iOS开发 传感器(加速计、摇一摇、计步器)
一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...
- iOS开发——高级篇——传感器(加速计、摇一摇、计步器)
一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...
- ios UIWindow 错误使用导致无法接收motionEnded(摇一摇)函数
今天遇到一个问题,第一次运行程序时,- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event函数无法调用,第二次就好了 ...
- iOS开发——高级技术&摇一摇功能的实现
摇一摇功能的实现 在AppStore中多样化功能越来越多的被使用了,所以今天就开始介绍一些iOS开发的比较实用,但是我们接触的比较少的功能,我们先从摇一摇功能开始 在 UIResponder中存在这么 ...
- IOS端的摇一摇功能
//微信的摇一摇是怎么实现的~发现原来 ios本身就支持 //在 UIResponder中存在这么一套方法 - (void)motionBegan:(UIEventSubtype)motion wit ...
- IOS中微信摇一摇声音无法播放解决办法
在IOS中第一次调用play方法播放音频会被阻止,必须得等用户有交互动作,比如touchstart,click后才能正常调用,所以可以在摇一摇之前提醒用户点击一下开始游戏的按钮或者给用户一个弹窗,用户 ...
- IOS 摇一摇的方法
● 监控摇一摇的方法 ● 方法1:通过分析加速计数据来判断是否进行了摇一摇操作(比较复杂) ● 方法2:iOS自带的Shake监控API(非常简单) ● 判断摇一摇的步骤:实现3个摇一摇监听方法 ● ...
- swift 实现 iOS摇一摇
本博客包含了如何实现iOS摇一摇全步骤,包括了完整的代码. 先附上demo地址https://github.com/Liuyubao/LYBShake ,支持swift3.0+. 一.导包 项目主要使 ...
- 不会吧,这也行?iOS后台锁屏监听摇一摇
目录 背景介绍 探索过程 其他 APP 有没有类似功能 系统提供的摇一摇回调能否满足 其他方法能否实现 利用 CoreMotion 框架,监听加速计原始数据 通过加速计监听摇一摇 控制器相关逻辑和代码 ...
随机推荐
- 【poj2411】Mondriaan's Dream 状态压缩dp
AC传送门:http://vjudge.net/problem/POJ-2411 [题目大意] 有一个W行H列的广场,需要用1*2小砖铺盖,小砖之间互相不能重叠,问有多少种不同的铺法? [题解] 对于 ...
- js 滚动条滚动到底部触发事件
一.前言 在开发项目时,常常需要展示大量数据.如果全部显示出来,数据相对少时,看不出来什么不同,如果数据很多时,一次请求全部显示,这就相当可怕了. 面对这种问题,PC里使用了分页效果,将数据分成一页页 ...
- 矩阵乘法np.dot()及np.multiply()以及*
转载自 https://blog.csdn.net/u012609509/article/details/70230204 Python中的几种矩阵乘法 1. 同线性代数中矩阵乘法的定义: np.do ...
- (广搜)聪明的打字员 -- POJ --1184
链接: http://poj.org/problem?id=1184 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#probl ...
- [转]WCF体系结构-一张图就是好
本文转自:http://www.cnblogs.com/snakevash/archive/2011/05/02/2034414.html 今天在MSDN上面看到了这么一张图,让我顿时感觉脑袋清醒很多 ...
- android事件分发
1). android对事件分发的顺序为:Activity--->PhoneWindow--->DecorView--->yourView; 2). android控件对事件处理的优 ...
- vue实现首页导航切换不同路由的方式(二)【使用vuex实现的】
<nav> <!-- 导航栏 --> <div class="indexNavOut"> <div class="indexNa ...
- nutch-2.2.1 hadoop-1.2.1 hbase-0.92.1 集群部署(实用)
原文地址: http://www.cnblogs.com/i80386/p/3540389.html 参考网站:http://blog.csdn.net/weijonathan/article/det ...
- 使用Toolbar + DrawerLayou实现菜单侧滑,改变toolbar左上角图标
侧边栏具体实现可以参照http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0303/2522.html getSupportActio ...
- 自己从0开始学习Unity的笔记 VI (C#的for循环练习)
最近学到了for循环,我觉得其实看情况吧,和while挺像的,不过适合于累加或者累减这类的,for循环要更好用一点 for循环首先格式是 ; i < length; i++) { } 意思很简单 ...