【问题标题】:Objective C & iOS: running a timer? NSTimer/Threads/NSDate/etcObjective C & iOS:运行定时器? NSTimer/线程/NSDate/等
【发布时间】:2012-01-28 06:29:06
【问题描述】:

我正在开发我的第一个 iOS 应用程序,并且遇到了第一个问题,但我无法找到一个好的答案。

问题:我有一个自定义的UIGestureRecognizer 并已正确连接,我可以在识别后为@selector 中的每个触摸运行代码。这对大多数事情来说都很好,但对于其他人来说,这有点太多了。

我的目标:制作一个在指定时间间隔触发以运行逻辑的计时器,并能够在触摸被取消时取消它。

我在这里问的原因:解决方案有很多可能性,但没有一个是最适合实施的。目前看来

  • performSelector(以及一些变体)
  • NSThread
  • NSTimer
  • NSDate
  • 操作队列
  • 我想我也找到了其他一些...

从所有研究来看,某种形式的线程似乎是可行的方法,但我不知道哪种方法最适合这种情况。

实现示例:每 0.10 秒取一次NSPoint,取上一点与当前点之间的距离。 [计算每个点之间的距离会产生非常混乱的结果]。

相关代码:

- (void)viewDidLoad {
 CUIVerticalSwipeHold *vSwipe =
 [[CUIVerticalSwipeHold alloc]
 initWithTarget:self 
 action:@selector(touchHoldMove:)];
 [self.view addGestureRecognizer:vSwipe];
 [vSwipe requireGestureRecognizerToFail:doubleTap];
}

...

- (IBAction)touchHoldMove:(UIGestureRecognizer *)sender {
     if (sender.state == UIGestureRecognizerStateEnded) {

     }

     if (sender.state == UIGestureRecognizerStateBegan) {

     }

     //other stuff to do goes here

}

【问题讨论】:

    标签: objective-c ios nsdate nstimer nsthread


    【解决方案1】:

    为了完整起见,我在这里添加了我的最终实现(不确定这是不是这样做的方法,但在这里)

    .h

    @interface {
        NSTimer *myTimer;
    }
    
    @property (nonatomic, retain) NSTimer *myTimer;
    

    .m

    @synthesize myTimer;   
    
    -------------------------------------------
    
    - (void)viewDidLoad {
     //Relevant snipet
     CUIVerticalSwipeHold *vSwipe =
     [[CUIVerticalSwipeHold alloc]
     initWithTarget:self 
     action:@selector(touchHoldMove:)];
     [self.view addGestureRecognizer:vSwipe];
     [vSwipe requireGestureRecognizerToFail:doubleTap];
     }
    
    -------------------------------------------
    
    - (IBAction)touchHoldMove:(UIGestureRecognizer *)sender {
         if (sender.state == UIGestureRecognizerStateEnded) {
             //Cancel the timer when the gesture ends
             if ([myTimer isValid])
                {
                    [myTimer invalidate];
                }
             }
         }
    
         if (sender.state == UIGestureRecognizerStateBegan) {
            //starting the timer when the gesture begins
            myTimer = [NSTimer scheduledTimerWithTimeInterval:someTimeIncrement  
                                                       target:self
                                                     selector:@selector(someSelector) 
                                                     userInfo:nil 
                                                      repeats:YES];
         }
    }
    

    【讨论】:

      【解决方案2】:

      使用 NSTimer

      这样设置:

          theTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(yourMethodThatYouWantRunEachTimeTheTimerFires) userInfo:nil repeats:YES];
      

      然后当你想取消它时,做这样的事情:

          if ([theTimer isValid])
      {
          [theTimer invalidate];
      }
      

      请注意,在上面的示例中,您需要声明 NSTimer 的“theTimer”实例,这两种方法都可以使用它。在上面的例子中,“0.5”表示定时器每秒触发两次。根据需要进行调整。

      【讨论】:

      • 注意:在定时器失效后将定时器设置为 nil 被认为是一种很好的做法:[theTimer invalidate]; theTimer = nil;
      猜你喜欢
      • 2011-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-20
      • 1970-01-01
      • 2014-08-15
      • 1970-01-01
      相关资源
      最近更新 更多