touchesBegin延迟

touchesBegan with delay

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

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

它工作但很慢。通过点击视图,我需要 0.5 - 1.0 秒才能看到绿色。

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

您应该按照 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];
    }
}

试试这个:

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

注: 它会工作得更快。