我有一个小的UIImageView,我正在使用UIPanGestureRecognizer在视图中平移。我同时运行一个选择器,它本质上充当一个定时器,通过每秒递减1,更新UILabel以反映更改,并再次调用自己直到int达到0.问题是每次调用updateTime方法它取消了平移手势,UIImageView返回到主视图中的原始位置。
我尝试在新线程中分离updateTime方法,但在第一次调用后它没有递归调用自身。我也尝试使用UIButtons代替UIImageViews并调用 - (void)wasDragged:(UIButton *)按钮withEvent:(UIEvent *)事件方法来处理按钮框架更改,但这也没有解决问题。如何在平移时保持计时器运行?
以下是UIPanGestureRecognizer的代码:
- (IBAction)imagePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
计时器:
- (void) updateTime {
if (!running) return;
timerLabel.text = [NSString stringWithFormat:@"%u", timeLeft];
timeLeft--;
if (timeLeft == 0) {
running = false;
timerLabel.text = @"";
return;
}
[self performSelector:@selector(updateTime) withObject:self afterDelay:1];
谢谢!