ios 摇一摇 仿微信
2014-04-15 11:58
197 查看
微信的摇一摇是怎么实现的~发现原来 ios本身就支持
在 UIResponder中存在这么一套方法
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
这就是执行摇一摇的方法。那么怎么用这些方法呢?
很简单,你只需要让这个Controller本身支持摇动
同时让他成为第一相应者:
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[UIApplicationsharedApplication] setApplicationSupportsShakeToEdit:YES];
[selfbecomeFirstResponder];
}
然后去实现那几个方法就可以了
- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//检测到摇动
}
- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//摇动取消
}
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//摇动结束
if (event.subtype == UIEventSubtypeMotionShake) {
//something happens
}
}
@interface ShakeViewController : UIViewController<UIAccelerometerDelegate>
{
UIAccelerationValue myAccelerometer[3];
//是否响应摇一摇的标志
BOOL _canShake;
}
.m文件中
#define kFilteringFactor 0.1
#define kEraseAccelerationThreshold 2.0
@implementation ShakeViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
_canShake = YES;
}
return self;
}
- (void)dealloc
{
[UIAccelerometer sharedAccelerometer].delegate = nil;
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[UIAccelerometer sharedAccelerometer].delegate = self;
[UIAccelerometer sharedAccelerometer].updateInterval = 1.0f/40.0f;
}
#pragma mark - UIAccelerometerDelegate
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
UIAccelerationValue length, x, y, z;
if (!_canShake)
{
return;
}
//Use a basic high-pass filter to remove the influence of the gravity
myAccelerometer[0] = acceleration.x * kFilteringFactor + myAccelerometer[0] * (1.0 - kFilteringFactor);
myAccelerometer[1] = acceleration.y * kFilteringFactor + myAccelerometer[1] * (1.0 - kFilteringFactor);
myAccelerometer[2] = acceleration.z * kFilteringFactor + myAccelerometer[2] * (1.0 - kFilteringFactor);
// Compute values for the three axes of the acceleromater
x = acceleration.x - myAccelerometer[0];
y = acceleration.y - myAccelerometer[0];
z = acceleration.z - myAccelerometer[0];
//Compute the intensity of the current acceleration
length = sqrt(x * x + y * y + z * z);
// If above a given threshold, play the erase sounds and erase the drawing view
if(length >= kEraseAccelerationThreshold)
{
//是否响应摇一摇的标志
_canShake = NO;
[self shakeEvent];
}
}
1。 在App's Delegate中设定applicationSupportsShakeToEdit属性:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
application.applicationSupportsShakeToEdit= YES; //在ios6.0后,这里其实都可以不写了
self.window= [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
// Override point for customization after application launch.
self.viewController= [[ViewControlleralloc] initWithNibName:@"ViewController"bundle:nil];
self.window.rootViewController= self.viewController;
[self.windowmakeKeyAndVisible];
returnYES;
}
2。在你的View控制器中添加/重载canBecomeFirstResponder, viewDidAppear:以及viewWillDisappear:
//这里很重要,因为大部分视图 默认 的 canBecomeFirstResponder 是 NO的
-(BOOL)canBecomeFirstResponder {
return YES;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
-(void)viewWillDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
3。在你的view控制器中添加motionEnded:
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
if(motion == UIEventSubtypeMotionShake)
{
// your code
}
}
---------------------------------------------------------------
IOS 3.0 + 开始支持motion事件,检测设备摇动
– motionBegan:withEvent: 摇动开始时执行
– motionEnded:withEvent: 摇动结束时执行
– motionCancelled:withEvent: 摇动被取消时执行
在 UIResponder中存在这么一套方法
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
这就是执行摇一摇的方法。那么怎么用这些方法呢?
很简单,你只需要让这个Controller本身支持摇动
同时让他成为第一相应者:
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[UIApplicationsharedApplication] setApplicationSupportsShakeToEdit:YES];
[selfbecomeFirstResponder];
}
然后去实现那几个方法就可以了
- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//检测到摇动
}
- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//摇动取消
}
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
//摇动结束
if (event.subtype == UIEventSubtypeMotionShake) {
//something happens
}
}
IOS实现摇一摇源代码
@interface ShakeViewController : UIViewController<UIAccelerometerDelegate> {
UIAccelerationValue myAccelerometer[3];
//是否响应摇一摇的标志
BOOL _canShake;
}
.m文件中
#define kFilteringFactor 0.1
#define kEraseAccelerationThreshold 2.0
@implementation ShakeViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
_canShake = YES;
}
return self;
}
- (void)dealloc
{
[UIAccelerometer sharedAccelerometer].delegate = nil;
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[UIAccelerometer sharedAccelerometer].delegate = self;
[UIAccelerometer sharedAccelerometer].updateInterval = 1.0f/40.0f;
}
#pragma mark - UIAccelerometerDelegate
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
UIAccelerationValue length, x, y, z;
if (!_canShake)
{
return;
}
//Use a basic high-pass filter to remove the influence of the gravity
myAccelerometer[0] = acceleration.x * kFilteringFactor + myAccelerometer[0] * (1.0 - kFilteringFactor);
myAccelerometer[1] = acceleration.y * kFilteringFactor + myAccelerometer[1] * (1.0 - kFilteringFactor);
myAccelerometer[2] = acceleration.z * kFilteringFactor + myAccelerometer[2] * (1.0 - kFilteringFactor);
// Compute values for the three axes of the acceleromater
x = acceleration.x - myAccelerometer[0];
y = acceleration.y - myAccelerometer[0];
z = acceleration.z - myAccelerometer[0];
//Compute the intensity of the current acceleration
length = sqrt(x * x + y * y + z * z);
// If above a given threshold, play the erase sounds and erase the drawing view
if(length >= kEraseAccelerationThreshold)
{
//是否响应摇一摇的标志
_canShake = NO;
[self shakeEvent];
}
}
1。 在App's Delegate中设定applicationSupportsShakeToEdit属性:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
application.applicationSupportsShakeToEdit= YES; //在ios6.0后,这里其实都可以不写了
self.window= [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
// Override point for customization after application launch.
self.viewController= [[ViewControlleralloc] initWithNibName:@"ViewController"bundle:nil];
self.window.rootViewController= self.viewController;
[self.windowmakeKeyAndVisible];
returnYES;
}
2。在你的View控制器中添加/重载canBecomeFirstResponder, viewDidAppear:以及viewWillDisappear:
//这里很重要,因为大部分视图 默认 的 canBecomeFirstResponder 是 NO的
-(BOOL)canBecomeFirstResponder {
return YES;
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
-(void)viewWillDisappear:(BOOL)animated {
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
3。在你的view控制器中添加motionEnded:
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event
{
if(motion == UIEventSubtypeMotionShake)
{
// your code
}
}
---------------------------------------------------------------
IOS 3.0 + 开始支持motion事件,检测设备摇动
– motionBegan:withEvent: 摇动开始时执行
– motionEnded:withEvent: 摇动结束时执行
– motionCancelled:withEvent: 摇动被取消时执行
相关文章推荐
- 我是运营,我没有假期
- 如何做到日消息量100万的微信公众号?
- 论微信取消推送功能的可能性(原创)
- 微信的成功,靠的是QQ导流吗?
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- LCL.VBS 病毒源代码
- PHP限制页面只能在微信自带浏览器访问的代码
- 微信小程序去哪里找 小程序到底如何使用(附小程序名单)
- PHP实现微信公众平台音乐点播
- c#使用微信接口开发微信门户应用
- c#使用微信接口开发微信门户应用中微信消息的处理和应答
- Android中使用GridView实现仿微信图片上传功能(附源代码)
- Python查看微信撤回消息代码
- 3款值得推荐的微信开发开源框架
- PHP对接微信公众平台消息接口开发流程教程
- jQuery-digest | 咀嚼jQuery源码
- 掌握主流框架的源码实现,理解框架背后的思想(续) 3ff0
- 2048 C++源代码by Reason