2014-09-28 102 views
0

我有UIImageView的UIScrollview。在UIScrollView的内容上检测触摸事件

如果我在将UIImageView添加到scrollview之前添加手势,则不会触发touch事件。

我想得到一个水龙头发生在UIImageView。

我已经看到了一些关于从UIScrollView获取触摸点然后根据位置计算UIImageView的答案 - 但这看起来真的很混乱和矫枉过正。

是否有一种简单的方法从UIScrollView中的对象获取触摸事件?

回答

1

您的UIImageView s可能禁用了用户交互(这是他们默认使用的)。调用每个图像视图以下行,和你的水龙头应该承认:

yourImageView.userInteractionEnabled = YES; 

确保做到这一点before adding the gesture recognizer

几周前我有这个确切的问题,这是什么修复了我。

+0

BINGO!这完全是我需要的工作。点上感谢你! – Aggressor 2014-09-28 02:33:53

+0

乐意帮忙! :) – rebello95 2014-09-28 02:36:56

5

为了完整起见。

UIScrollView内部的UIImageView没有被编程为原生接收触摸事件和手势。

所以,你可能想要做的是:

  1. 添加UITapGestureRecognizer滚动视图用于启用滚动视图内的触摸。

>

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)]; 

singleTap.cancelsTouchesInView = NO; 

[myScrollView addGestureRecognizer:singleTap]; 
  • 定制的UIImageView与您从的UIImageView继承。
  • >

    CustomImageView *imageView = [[CustomImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
    [imageView setUserInteractionEnabled:YES]; 
    [scrollView addSubview:imageView]; 
    
  • 提供在自定义子类的触摸的方法和将其添加在您的UIScrollView ..
  • >

    @界面CustomImageView:UIImageView {}

    @implementation C ustomImageView

    - (id)initWithFrame:(CGRect)frame{} 
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {} 
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {} 
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {} 
    

    @end

    +0

    谢谢!我认为“singleTap.cancelsTouchesInView = NO;”是关键。 – green0range 2017-12-14 22:49:05