gpt4 book ai didi

objective-c - 如何使 Touch-Up-Inside 仅在正确控件中的实际 touch-up-inside 上触发?

转载 作者:搜寻专家 更新时间:2023-10-30 20:11:40 27 4
gpt4 key购买 nike

我有一个 3X3 UI 按钮矩阵,我希望允许用户拖动他的手指并根据手指离开触摸面板的位置触发正确的事件。

目前,如果手指触碰了某个按钮,内部的 touch-up 只会触发该按钮。

获得所需行为的最简单方法是什么?

最佳答案

我想这将取决于您是否希望按钮执行此修饰行为之外的其他事情。我假设您确实想要这个。

最灵活的行为可能来自附加到按钮父 View 的手势识别器。您所需要的只是几行代码来设置它和一个解释它的方法,因此如果您以后采用不同的方式,可以很容易地插入或删除它。

UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleDrag:)];

recognizer.cancelsTouchesInView = NO; //So the user can still interact with controls in the modal view
[self.view addGestureRecognizer:recognizer];
[recognizer release];

现在我们已经配置了识别器。我们如何处理它?<​​/p>

- (void)handleDrag:(UIPanGestureRecognizer *)sender
{
//We care only about touching *up*, so let's not bother checking until the gesture ends

if (sender.state == UIGestureRecognizerStateEnded)
{
CGPoint location = [sender locationInView:self.view];

//Iterate through your button collection

for (UIButton *button in self.buttons)
{

//Let's test to see if the point is inside this button
if (![button pointInside:[button convertPoint:location fromView:self.view] withEvent:nil])
{
//Do your thing
[self userTouchedUpOnButtonWithIndex:button.tag] //For example
//or
[self userTouchedUpOnButtonWithIndex:[self.buttons indexOfObject:button]];

break;
}
}
}

够简单吗?

关于objective-c - 如何使 Touch-Up-Inside 仅在正确控件中的实际 touch-up-inside 上触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6525988/

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