触动延迟
我有一个UIView
的子类,并添加了touchesBegan
和touchesEnd
方法…
在touchesBegan
,我使用self.backgroundColor = [UIColor greenColor]
…在touchesEnd
中将backgroundColor
从白色设置为绿色。将颜色重置为白色。
它的工作,但非常缓慢。 通过点击视图,需要0.5 – 1.0秒,直到看到绿色。
在UITableView
select一个单元格要快得多。
尝试这个:
self.view.userInteractionEnabled = YES; UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(doCallMethod:)]; recognizer.delegate = self; recognizer.minimumPressDuration = 0.0; [self.view addGestureRecognizer:recognizer]; - (void)doCallMethod:(UILongPressGestureRecognizer*)sender { if(sender.state == UIGestureRecognizerStateBegan){ NSLog(@"Begin"); self.view.backgroundColor = [UIColor greenColor]; }else if (sender.state == UIGestureRecognizerStateEnded){ NSLog(@"End"); self.view.backgroundColor = [UIColor whiteColor]; } }
注意:它将工作得更快。
您应该使用手势识别器作为TheBurgerShotbuild议,但我build议你一个UILongPressGestureRecognizer
。 就像是:
UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(changeColor:)]; gesture.minimumPressDuration = 0.f; [self.yourView addGestureRecognizer:gesture];
在你的viewDidLoad
。 和:
-(void) changeColor:(UIGestureRecognizer *)gestureRecognizer{ if (gestureRecognizer.state == UIGestureRecognizerStateBegan){ self.yourView.backgroundColor = [UIColor greenColor]; } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded){ self.yourView.backgroundColor = [UIColor whiteColor]; } }