Gan*_*ham 3 iphone cocoa-touch objective-c uiscrollview ios
在过去的四个小时里,我尝试了很多Stack Overlow解决方案,但没有一个能帮我解决问题.
这里是,
像这样的东西有效
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDidFire:)];
longPress.minimumPressDuration = 0.5;
[scroll addGestureRecognizer:longPress]; //scroll defined elsewhere
但是,如果我将滚动替换为滚动的任何子视图,则长按事件永远不会触发.
- 如何检测长按滚动视图的子视图?
- 然而,这是一个非常混乱的黑客,因为我可以检测到滚动视图的长按,有什么方法可以检测印刷机的位置,以便我可以看到正在按下哪个特定的子视图.
另外,(insert subview here).userInteractionEnabled = YES我为滚动视图的所有子视图设置了此属性,因此这应该不是问题.
我也尝试过使用touchesBegan和touchesEnded方法,如Stack Overflow中其他地方所建议的那样.
此外,对于图像视图,我使用for循环为每个自定义图像视图设置新的UILongPressGestureRecognizer,因为我知道每个手势识别器规则的1个视图.
来自第一次iOS开发者,
格雷厄姆
PS我真的更喜欢我能找到1的解决方案,而不是凌乱的2.
更多代码要求:
在Init视图控制器中
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDidFire:)];
longPress.minimumPressDuration = 0.5;
[self.titleLabel addGestureRecognizer:longPress]; //titleLabel property initialized elsewhere
[mainView addSubview:self.titleLabel];
Run Code Online (Sandbox Code Playgroud)
在"加载图像"方法中
for (NSData * dataImg in results) {
//Does some work turning data into an image, into an image view called img
img.delegate = self;
img.userInteractionEnabled = YES;
UILongPressGestureRecognizer *aLongPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDidFire:)];
aLongPress.minimumPressDuration = 0.5;
[img addGestureRecognizer:aLongPress];
[imgContainer addSubview:img];
}
Run Code Online (Sandbox Code Playgroud)
更多代码+笔记
self.view(UIView)
- >滚动(UIScrollView)
- > - > mainView(UIView)
- > - > - > titleLabel(UILabel)
- > - > - > imgContainer(UIView)
- > - > - > - > images(UIImageViews)
[self.view addSubview:scroll];
[scroll addSubview:mainView];
[mainView addSubview:self.titleLabel];
[mainView addSubview:imgContainer];
[imgContainer addSubview:img]; //done 8x via for loop
Run Code Online (Sandbox Code Playgroud)
感谢@ RegularExpression的回答,我现在知道mainView正在被按下,但不是它的子视图,所以我需要找到一种方法来显示它上面的mainView的子视图.:)
另一个更新,titleLabel工作.ImageViews仍然无法正常工作.:(
我知道这有点晚了,已经选择了一个答案,但是如果你有iOS7,别人想要一个很好的简单解决方案.
在你的UILongPressGestureRecognizer委托中,实现gestureRecognizer:shouldRequireFailureOfGestureRecognizer:otherGestureRecognizer选择器
检查otherGestureRecognizer是否为UIPanGestureRecognizer并返回YES,否则返回NO
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if ([otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
return YES;
}
return NO;
}
Run Code Online (Sandbox Code Playgroud)
滚动视图实际上会产生一个UIScrollViewPanGestureRecognizer,它是私有API的一部分,但它是UIPanGestureRecognizer的子类,所以上面的工作正常.
为了支持iOS6的或以下,那么你会通过的UIScrollView的gestureRecognizers需要循环,检测其中一个是UIPanGestureRecognizer并执行requireGestureRecognizerToFail您选择UILongPressGestureRecogizer这一点.
| 归档时间: |
|
| 查看次数: |
5695 次 |
| 最近记录: |