【发布时间】:2013-03-30 01:01:03
【问题描述】:
在过去的四个小时里,我尝试了许多 Stack Overlow 解决方案,但都没有解决我的问题。
来了,
- 我有一个 UIScrollView
- 在该 ScrollView 中有 一个自定义 UILabel 和 8 个自定义 UIImageViews
- 我想检测长按
-
类似这样的方法
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDidFire:)];
longPress.minimumPressDuration = 0.5; [scroll addGestureRecognizer:longPress]; //scroll defined elsewhere
但是,如果我将滚动替换为滚动的任何子视图,则长按事件永远不会触发。
- 如何检测长按滚动视图的子视图?
- 这是一个相当混乱的 hack,但是,由于我可以检测到滚动视图的长按,有什么方法可以检测到 印刷机的位置,以便我可以看到哪个特定的子视图 被按下。
另外,(insert subview here).userInteractionEnabled = YES,我为滚动视图的所有子视图设置了这个属性,所以这应该不是问题。
我也尝试过使用 Stack Overflow 中其他地方建议的 touchesBegan 和 touchesEnded 方法。
此外,对于图像视图,我确实使用 for 循环为每个自定义图像视图设置了一个新的 UILongPressGestureRecognizer,因为我知道每个手势识别器 1 个视图规则。
来自第一次 iOS 开发者,
格雷厄姆
附:如果我能找到 1. 而不是凌乱的 2. 的解决方案,我真的更喜欢。
应要求提供更多代码:
在视图控制器的初始化中
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];
在“加载图像”方法中
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];
}
更多代码 + 注释
self.view (UIView)
->滚动(UIScrollView)
->->主视图(UIView)
->->->titleLabel(UILabel)
->->->imgContainer (UIView)
->->->->图像(UIImageViews)
[self.view addSubview:scroll];
[scroll addSubview:mainView];
[mainView addSubview:self.titleLabel];
[mainView addSubview:imgContainer];
[imgContainer addSubview:img]; //done 8x via for loop
感谢@RegularExpression 的回答,我现在知道 mainView 被按下了,但不是它的子视图,所以我需要找到一种方法来在它上面显示 mainView 的子视图。 :)
另一个更新,titleLabel 有效。 ImageViews 仍然不起作用。 :(
【问题讨论】:
-
你如何将
subViews添加到UIScrollView? -
正在加载代码,请稍等。谢谢您的帮助。 @老虎
-
对不起,我之前误解了你,现在已经发布了我如何将视图添加到我的滚动视图@TheTiger
标签: iphone ios objective-c cocoa-touch uiscrollview