使用 UIGestureRecognizer 模拟按钮单击

Posted

技术标签:

【中文标题】使用 UIGestureRecognizer 模拟按钮单击【英文标题】:Imitating button click with UIGestureRecognized 【发布时间】: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];
    

【问题讨论】:

【参考方案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];

【讨论】:

以上是关于使用 UIGestureRecognizer 模拟按钮单击的主要内容,如果未能解决你的问题,请参考以下文章

使用 UIGestureRecognizer 旋转瓶子

如何用 UIGestureRecognizer 替换 TouchesBegan

UIGestureRecognizer 从头开始​​制作 UISlider

`UIGestureRecognizer`命中测试

UIGestureRecognizer 键值编码

将 SwiftUI 与多个 UIGestureRecognizer 一起使用?