首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >延迟touchesBegan

延迟touchesBegan
EN

Stack Overflow用户
提问于 2015-08-05 09:43:57
回答 2查看 1.8K关注 0票数 7

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

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

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

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-05 10:04:43

试试这个:

代码语言:javascript
复制
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];
    }
}

注意:会工作得更快。

票数 4
EN

Stack Overflow用户

发布于 2015-08-05 09:58:33

您应该按照TheBurgerShot的建议使用手势识别器,但我建议您使用UILongPressGestureRecognizer。类似于:

代码语言:javascript
复制
UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(changeColor:)];
gesture.minimumPressDuration = 0.f;
[self.yourView addGestureRecognizer:gesture];

在你的viewDidLoad里。和:

代码语言:javascript
复制
-(void) changeColor:(UIGestureRecognizer *)gestureRecognizer{

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
        self.yourView.backgroundColor = [UIColor greenColor];
    }
    else if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
        self.yourView.backgroundColor = [UIColor whiteColor];
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31828857

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档