gpt4 book ai didi

objective-c - 检测长按 UINavigationItem 的后退按钮

转载 作者:技术小花猫 更新时间:2023-10-29 10:14:35 26 4
gpt4 key购买 nike

我想通过我的基于 UINavigationController 的应用程序为我的后退按钮添加功能,长按后退按钮将弹出到根目录。但是,我不知道在哪里附加手势识别器。我是否将 UINavigationBar 子类化并尝试检测长按是否在左侧按钮区域?

我以前听说过有人添加了类似的功能。有人有什么想法吗?

最佳答案

我知道这个问题很老了,但我想出了一个解决方案。我没有尝试将手势识别器添加到按钮本身(这将是理想的),而是将其添加到 self.navigationController.navigationBar 中,然后在操作方法中使用 locationInView 看看我是否在后退按钮上。我不完全确定如何精确识别后退按钮,所以我笨拙地捕获第一个 x 坐标小于某个任意值的 subview ,但它看起来很有希望。如果有人有更好的方法来识别后退按钮的框架,请告诉我。

- (void)longPress:(UILongPressGestureRecognizer *)sender 
{
if (sender.state == UIGestureRecognizerStateEnded)
{
// set a default rectangle in case we don't find the back button for some reason

CGRect rect = CGRectMake(0, 0, 100, 40);

// iterate through the subviews looking for something that looks like it might be the right location to be the back button

for (UIView *subview in self.navigationController.navigationBar.subviews)
{
if (subview.frame.origin.x < 30)
{
rect = subview.frame;
break;
}
}

// ok, let's get the point of the long press

CGPoint longPressPoint = [sender locationInView:self.navigationController.navigationBar];

// if the long press point in the rectangle then do whatever

if (CGRectContainsPoint(rect, longPressPoint))
[self doWhatever];
}
}

- (void)addLongPressGesture
{
if (NSClassFromString(@"UILongPressGestureRecognizer"))
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.navigationController.navigationBar addGestureRecognizer:longPress];
[longPress release];
}
}

关于objective-c - 检测长按 UINavigationItem 的后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6509155/

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