【问题标题】:Imitating button click with UIGestureRecognized使用 UIGestureRecognizer 模拟按钮单击
【发布时间】:2013-03-24 13:10:56
【问题描述】:

将UITapGestureRecognized 添加到UIView 后,我如何解析视图的ie。在点击期间更改背景颜色?这样做的目的是模仿按钮点击。

UIView *locationView = [[UIView alloc] init];
locationView.tag = 11;
locationView.backgroundColor = [UIColor clearColor];
locationView.userInteractionEnabled = YES;
[locationView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(promptForLocation)]];

假设我想在点击手势之后立即locationView.backgroundColor = [UIColor blueColor]。你会在目标操作中实现它还是有一个具体的实现?

更新: 这是我受@0x7fffffff 启发的最终代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // ...
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDetectedLocation:)];
    longPress.allowableMovement = 50.0f;
    longPress.minimumPressDuration = 0.05;
    UILongPressGestureRecognizer *longPress2 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDetectedPhoto:)];
    longPress2.allowableMovement = 50.0f;
    longPress2.minimumPressDuration = 0.05;
    [leftView addGestureRecognizer:longPress];
    [rightView addGestureRecognizer:longPress2];
    // ...
}

- (BOOL)longPressDetected:(UILongPressGestureRecognizer *)sender {
    if ([self.view hasFirstResponder]) {
        return NO;
    }
    if (sender.state == UIGestureRecognizerStateBegan) {
        [sender.view setBackgroundColor:[UIColor colorWithRed:(4/255.0) green:(129/255.0) blue:(241/255.0) alpha:1]];
    } else if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateFailed) {
        [sender.view setBackgroundColor:[UIColor clearColor]];
    }
    CGPoint location = [sender locationInView:sender.view];
    return sender.state == UIGestureRecognizerStateEnded && location.x > 0 && location.x < sender.view.frame.size.width && location.y > 0 && location.y < sender.view.frame.size.height;
}

- (void)longPressDetectedLocation:(UILongPressGestureRecognizer *)sender {
    if ([self longPressDetected:sender]) {
        [self promptForLocation];
    }
}

- (void)longPressDetectedPhoto:(UILongPressGestureRecognizer *)sender {
    if ([self longPressDetected:sender]) {
        [self promptForPhoto];
    }
}

【问题讨论】:

    标签: ios objective-c cocoa-touch uiview uigesturerecognizer


    【解决方案1】:

    考虑到您正在尝试模拟按钮单击,我假设您希望视图在触摸结束后恢复到其原始状态。为此,您需要使用UILongPressGestureRecognizer 而不是UITapGestureRecognizer。

    使用轻击手势,直到触摸结束才会检测到识别器,因此您一抬起手指就可以有效地突出显示视图。要解决此问题,请使用长按手势,将其minimumPressDuration 属性设置为 0.0。然后在其选择器中,检查发送手势的状态;如果它刚刚开始,则更改背景颜色,如果它已经结束,则恢复为原始颜色。

    这是一个例子:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(100.0f, 100.0f, 100.0f, 100.0f)];
        [myView setBackgroundColor:[UIColor redColor]];
        [self.view addSubview:myView];
    
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDetected:)];
        [longPress setAllowableMovement:50.0f];
        [longPress setMinimumPressDuration:0.0];
        [myView addGestureRecognizer:longPress];
    }
    
    - (void)longPressDetected:(UILongPressGestureRecognizer *)sender {
        if (sender.state == UIGestureRecognizerStateBegan) {
            [sender.view setBackgroundColor:[UIColor blueColor]];
        }else if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateFailed) {
            [sender.view setBackgroundColor:[UIColor redColor]];
        }
        NSLog(@"%d",sender.state);
    }
    

    【讨论】:

    • @CasparAleksanderBangJespers 很高兴为您提供帮助!
    【解决方案2】:
    UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedMethod:)];
        [locationView addGestureRecognizer:gr];
    

    这是方法

    -(void)tappedMethod:(UIGestureRecognizer *)ge
    {
      // write relavent code here;
      locationView.backgroundColor = [UIColor blueColor];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      • 2020-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多