gpt4 book ai didi

ios - 使用 UIGestureRecognizer 模拟按钮点击

转载 作者:行者123 更新时间:2023-11-28 19:10:55 25 4
gpt4 key购买 nike

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

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

最佳答案

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

使用轻击手势,直到触摸结束才会检测到识别器,因此您一抬起手指就可以有效地突出显示 View 。要解决此问题,请使用长按手势,将其 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);
}

关于ios - 使用 UIGestureRecognizer 模拟按钮点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15598869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com