iOS 微信摇一摇的实现

微信的摇一摇是怎么实现的,在 UIResponder中存在这么一套方法

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)even __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];

[[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

}

}

swift中对应的代码

 application.applicationSupportsShakeToEdit = true
 self.becomeFirstResponder()

对应的方法

 override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?) {
    super.motionBegan(motion, withEvent: event)
    UIApplication.sharedApplication().keyWindow?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
}

override func motionCancelled(motion: UIEventSubtype, withEvent event: UIEvent?) {
    super.motionCancelled(motion, withEvent: event)
}

override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {
    super.motionEnded(motion, withEvent: event)
}

注意,测试用真机哦,别用电脑摇来摇去-。-

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 文/冷冽无极 双十一了,你的手还在吗? 又到了一年一次的双十一了。曾经的它也许只是光棍们畅想爱情的节日,而现在则成...
    一诺花开阅读 337评论 11 3
  • 2017.8.7 星期一 晴 忙了一天,傍晚回到家。看到儿子练的字,有进步。 洗完衣服,又读课外书去了,现...
    暖与希望阅读 222评论 0 1
  • 关于逝去的生命 我只为有生命的惋惜 关于逝去的时光 我只因有故事而挽留 逝去种种 无羁无冗 绕指间柔 无念无求 谁...
    梦魔公主阅读 80评论 3 5
  • I would rather share one lifetime with you than face all ...
    玉覃秋阅读 575评论 0 1
  • 之前用的技术 团队之前采用的是ext.js + SpringMVC前后端统一的开发方式。为什么会用这种开发方式呢?...
    单调先生阅读 610评论 1 0