触动开始延迟

时间:2015-08-05 09:43:58

标签: ios objective-c gesture uitapgesturerecognizer

我有UIView的子类,并添加了touchesBegantouchesEnd方法......

touchesBegan中,我使用backgroundColorself.backgroundColor = [UIColor greenColor]从白色设置为绿色... touchesEnd我将颜色重置为白色。

它的工作原理非常缓慢。通过点击视图,它需要0.5 - 1.0秒,直到我看到绿色。

选择UITableView中的单元格要快得多。

2 个答案:

答案 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];
    }
}