touchesBegan with delay

梦想的初衷 提交于 2019-12-08 20:08:48

问题


I have a subclass of UIView, and added the touchesBegan and touchesEnd methods...

In touchesBegan, I set the backgroundColor from white to green by using self.backgroundColor = [UIColor greenColor] ... in the touchesEnd I reset the color to white.

It works but very slowly. By tapping the view, it takes 0.5 - 1.0 sec until I see the green color.

Selecting a cell in a UITableView it's much faster.


回答1:


Try this:

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

Note: It will work more faster.




回答2:


You should use a gesture recognizer as TheBurgerShot suggested but I recommend you an UILongPressGestureRecognizer. Something like:

UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(changeColor:)];
gesture.minimumPressDuration = 0.f;
[self.yourView addGestureRecognizer:gesture];

in your viewDidLoad. And:

-(void) changeColor:(UIGestureRecognizer *)gestureRecognizer{

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
        self.yourView.backgroundColor = [UIColor greenColor];
    }
    else if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
        self.yourView.backgroundColor = [UIColor whiteColor];
    }
}


来源:https://stackoverflow.com/questions/31828857/touchesbegan-with-delay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!