0

Having added a UITapGestureRecognized to a UIView, how do I parse the view for ie. changing the background color during the tap? The purpose of this is to imitate a button click.

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

Let's say I want locationView.backgroundColor = [UIColor blueColor] right after the tap gesture. Would you just implement it in the target action or is there a specific implementation for this?

Update: This is my final code inspired by @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];
    }
}
4

2 回答 2

3

考虑到您正在尝试模仿按钮单击,我假设您希望视图在触摸结束后恢复到其原始状态。为此,您需要使用 aUILongPressGestureRecognizer而不是 a 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);
}
于 2013-03-24T13:36:16.597 回答
1
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedMethod:)];
    [locationView addGestureRecognizer:gr];

这是方法

-(void)tappedMethod:(UIGestureRecognizer *)ge
{
  // write relavent code here;
  locationView.backgroundColor = [UIColor blueColor];
}
于 2013-03-24T13:19:08.663 回答