首页 > 其他 > 详细

iOS开发笔记之摇动手势

时间:2014-02-09 15:37:42      阅读:265      评论:0      收藏:0      [点我收藏+]

1.当设备摇动时,系统会算出加速计的值,并告知是否发生了摇动手势。系统只会运动开始和结束时通知你,并不会在运动发生的整个过程中始终向你报告每一次运动。例如,你快速摇动设备三次,那只会收到一个摇动事件。

2,想要实现摇动手势,首先需要使视图控制器成为第一响应者,注意不是单独的控件。成为第一响应者最恰当的时机是在视图出现的时候,而在视图消失的时候释放第一响应者。

bubuko.com,布布扣
-(BOOL)canBecomeFirstResponder
{
    return YES;
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [self resignFirstResponder];
    [super viewWillDisappear:animated];
}
bubuko.com,布布扣

现在,视图控制器准备处理运动事件,其中有三个方法,motionBegan,motionEnded和motionCancelled,这些与触摸方法有着相似的工作原理,但没有移动方法。

bubuko.com,布布扣
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (event.type == UIEventSubtypeMotionShake)
    {
        NSLog(@"Shake Began");
    }
}

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (event.type == UIEventSubtypeMotionShake)
    {
        NSLog(@"Shake End");
    }
}

-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if (event.type == UIEventSubtypeMotionShake)
    {
        NSLog(@"Shake Cancelled");
    }
}
bubuko.com,布布扣

转载请注明原著:http://www.cnblogs.com/marvindev

下一篇翻译一篇文章:让我们构造UITableView

 

iOS开发笔记之摇动手势

原文:http://www.cnblogs.com/marvindev/p/motion.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!