我有UIView
的子类,并添加了touchesBegan
和touchesEnd
方法......
在touchesBegan
中,我使用backgroundColor
将self.backgroundColor = [UIColor greenColor]
从白色设置为绿色... touchesEnd
我将颜色重置为白色。
它的工作原理非常缓慢。通过点击视图,它需要0.5 - 1.0秒,直到我看到绿色。
选择UITableView
中的单元格要快得多。
答案 0 :(得分:4)
试试这个:
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];
}
}
注意:强> 它会更快地工作。
答案 1 :(得分:2)
你应该使用手势识别器作为TheBurgerShot建议,但我建议你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];
}
}