使用UILongPressGestureRecognizer UIScrollview的子视图 [英] Using UILongPressGestureRecognizer For Subviews of UIScrollview

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

问题描述

在过去四个小时,我尝试过许多Stack Overlow解决方案,但没有一个帮助解决我的问题。



这里是




  • 我有一个 UIScrollView

  • 在此ScrollView中有一个自定义UILabel和8个自定义UIImageView

  • 我想检测长按

  • 这样的工作



    UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDidFire :)];

    longPress.minimumPressDuration = 0.5;
    [scroll addGestureRecognizer:longPress]; // scroll defined elsewhere其他地方




滚动,长按事件不会触发。



  1. 如何检测长按子视图滚动视图?

  2. 这是一个很杂乱的黑客,但是,因为我可以检测长按滚动视图,有没有任何方式,我可以检测
    位置的按,以便我可以看到哪个特定子视图是
    被按下。


(insert subview here).userInteractionEnabled = YES ,我为滚动视图的所有子视图设置此属性,所以这不应该是一个问题。



我也尝试过使用touchesBegan和touchesEnded方法,如Stack Overflow中的建议。



此外,对于图像视图,一个新的UILongPressGestureRecognizer为每个自定义图像视图,使用一个for循环,因为我知道1视图每个手势识别器规则。



第一次iOS开发人员,



Graham



PS我真的很喜欢,如果我能找到一个解决方案1.而不是凌乱的2。








在视图控制器的Init中

  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 = self;
img.userInteractionEnabled = YES;
UILongPressGestureRecognizer * aLongPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDidFire :)];
aLongPress.minimumPressDuration = 0.5;
[img addGestureRecognizer:aLongPress];
[imgContainer addSubview:img];
}









- > scroll(UIScrollView) / p>

- > - > mainView(UIView)



- > - > - > titleLabel >

- > - > - > imgContainer(UIView)



- > - > - > - > / p>



  [self.view addSubview:scroll]; 
[scroll addSubview:mainView];
[mainView addSubview:self.titleLabel];
[mainView addSubview:imgContainer];
[imgContainer addSubview:img]; //通过for循环完成8x


$ b

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



另一个更新,titleLabel工作。 ImageViews仍然不工作。 :(

解决方案

Woohoo it works!



问题是:



imgContainer是具有多个UIImageView作为子视图的具有空框架的UIView



我的印象是,当我向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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆