对 UIScrollview 的子视图使用 UILongPressGestureRecogni

Using UILongPressGestureRecognizer For Subviews of UIScrollview(对 UIScrollview 的子视图使用 UILongPressGestureRecognizer)
本文介绍了对 UIScrollview 的子视图使用 UILongPressGestureRecognizer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的四个小时里,我尝试了许多 Stack Overlow 解决方案,但都没有解决我的问题.

来了,

  • 我有一个 UIScrollView
  • 在该 ScrollView 中有 一个自定义 UILabel 和 8 个自定义 UIImageViews
  • 我想检测长按
  • 类似这样的方法

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDidFire:)];
    longPress.minimumPressDuration = 0.5;[滚动添加手势识别器:长按];//滚动在别处定义

但是,如果我将滚动替换为滚动的任何子视图,则长按事件永远不会触发.

<块引用>

  1. 如何检测长按滚动视图的子视图?
  2. 这是一个相当混乱的 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 属性在别处初始化[mainView addSubview:self.titleLabel];

在加载图像"方法中

for (NSData * dataImg in results) {//做一些工作将数据转换为图像,转换为名为 img 的图像视图img.delegate = 自我;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];[滚动添加子视图:主视图];[mainView addSubview:self.titleLabel];[主视图添加子视图:imgContainer];[imgContainer addSubview:img];//通过for循环完成8x

感谢@RegularExpression 的回答,我现在知道 mainView 被按下了,但不是它的子视图,所以我需要找到一种方法来在它上面显示 mainView 的子视图.:)

另一个更新,titleLabel 有效.ImageViews 仍然不起作用.:(

解决方案

哇哦!

问题是:

imgContainer 是一个 带有空框架的 UIView,其中有几个 UIImageViews 作为子视图

我的印象是,当我向 imgContainer 添加子视图时,imgContainer 会展开.

不是真的.

我必须将 imgContainer 的框架设置为与滚动视图相同的内容框架,然后一切正常.

我希望这个答案可以帮助像我这样的任何其他未来的 iOS 初学者.

For the past four hours, I have tried many Stack Overlow solutions but none have helped solve my problem.

Here it is,

  • I have a UIScrollView
  • Within that ScrollView there is one custom UILabel and 8 custom UIImageViews
  • I want to detect a long press
  • Something like this works

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDidFire:)];
    longPress.minimumPressDuration = 0.5; [scroll addGestureRecognizer:longPress]; //scroll defined elsewhere

However, if I replace the scroll with any subviews of scroll, the long press event never fires.

  1. How do I detect a long press of a subview of a scroll view?
  2. This is quite a messy hack, however, since I can detect long presses of a scroll view, is there any way where I can detect the position of the press so that I can see which specific subview is being pressed.

Also, (insert subview here).userInteractionEnabled = YES, I set this property for all my subviews of the scroll view, so this should not be a problem.

I have also tried using touchesBegan and touchesEnded method as suggested elsewhere in Stack Overflow.

Also, for the image views, I do set a new UILongPressGestureRecognizer for every custom image view, using a for loop, as I am aware of the 1 view per gesture recognizer rule.

From A First Time iOS Developer,

Graham

P.S. I'd really prefer if I could find a solution for 1. rather than the messy 2.


More Code As Requested:

In the Init of the view Controller

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

In a "load images" method

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


Even More Code + Notes

self.view (UIView)

->scroll (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

Thanks to @RegularExpression's answer, I now am aware that the mainView is getting pressed, but not its subviews, so I need to find a way to display the mainView's subviews above it. :)

Another update, titleLabel works. ImageViews still don't work though. :(

解决方案

Woohoo it works!

The problem was:

imgContainer was a UIView with an empty frame with several UIImageViews as subviews

I was under the impression that as I added a subview to imgContainer, imgContainer would expand.

This is not true.

I had to set imgContainer's frame to the same content frame as the scroll view and then everything became ok.

I hope this answer helps any other future iOS firs timers like me.

这篇关于对 UIScrollview 的子视图使用 UILongPressGestureRecognizer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)