简体   繁体   English

将触摸手势发送到UIScrollView的UIImageView子视图

[英]Sending touch gestures to UIImageView subview of UIScrollView

I have programmed a UIImageView that allows me to draw inside of it. 我编写了一个UIImageView,可以在其中绘制内容。 It therefore tracks the users touches and records it. 因此,它跟踪并记录用户的触摸。 When used in a window it works great. 在窗口中使用时,效果很好。

However, I have then added it as a subView of a UIScrollView which resides in a View Controller. 但是,然后我将其添加为驻留在视图控制器中的UIScrollView的子视图。 When I try and use it now, the touch gestures inside of the UIImageView simply scroll the whole view rather than draw inside the UIImageView. 当我现在尝试使用它时,UIImageView内部的触摸手势只是滚动整个视图,而不是在UIImageView内部绘制。

How do I refer gestures made to the UIScrollView which are also inside of the UIImageView to the UIImageView. 如何将同样位于UIImageView内部的对UIScrollView所做的手势引用到UIImageView。

I hope that makes sense 我希望这是有道理的

Best regards 最好的祝福

EDIT: I have set all the following properties: canCancelContentTouches , exclusiveTouch and delaysContentTouches to NO. 编辑:我已经将所有以下属性: canCancelContentTouchesexclusiveTouchdelaysContentTouches为NO。

Now, when I touch inside the UIImageView it doesn't scroll but still won't call the methods: touchesBegan:withEvent: , touchesMoved:withEvent: or touchesEnded:withEvent: which each reside in the ViewController 现在,当我触摸UIImageView时,它不会滚动,但仍不会调用以下方法: touchesBegan:withEvent:touchesMoved:withEvent:touchesEnded:withEvent:分别位于ViewController中

设置exclusiveTouch的的UIImageView的财产YES (这意味着,当的UIImageView被触摸时,那一抹不会对任何其他视图效果)

How do you want the app to decide whether a touch is supposed to scroll the scroll view or draw in the image view? 您希望应用程序如何决定是触摸滚动视图还是在图像视图中绘制?

Let's say you want one finger to draw and two fingers to scroll. 假设您要一根手指来画,而两根手指要滚动。 If you're targetting iOS 5.0, it's easy: 如果您的目标是iOS 5.0,则很简单:

self.scrollView.panGestureRecognizer.minimumNumberOfTouches = 2;

If you're targetting an earlier iOS, you can't use the panGestureRecognizer property. 如果您要定位的是较早的iOS,则不能使用panGestureRecognizer属性。 You have to dig through the scroll view's gestureRecognizers property to find the right recognizer. 您必须在滚动视图的gestureRecognizers属性中进行挖掘以找到正确的识别器。 Check out my answer here for example code. 在此处查看我的答案以获取示例代码。

@robmayoff is right that dragging a subview of a scroll view is ambiguous, since it's tough to know what the user intends to drag... and two fingers is one way of differentiating meaning. @robmayoff是正确的,因为拖动滚动视图的子视图是模棱两可的,因为很难知道用户打算拖动什么...而用两根手指是区分含义的一种方式。

I don't favor two fingers, because I don't think it's a well known gesture on iOS. 我不赞成使用两个手指,因为我认为这不是iOS上众所周知的手势。 After one adds it, the next step is to add UI which explains it. 添加后,下一步是添加解释它的UI。 Whenever I find myself needing to do that, I begin to wonder if I made a design mistake. 每当我发现自己需要这样做时,我就会开始怀疑自己是否犯了设计错误。

Another way to distinguish users' drag intent is by comparing the direction of the drag to the orientation of the scroll view. 区分用户拖动意图的另一种方法是将拖动的方向与滚动视图的方向进行比较。 If user drags along the axis of a (non-square) scroll view, the drag can be interpreted to mean a drag of the scroll view . 如果用户沿(非正方形)滚动视图的轴拖动,则该拖动可以解释为表示滚动视图拖动 If they're touching a sub-view and dragging perpendicularly to the axis, then treat it as a drag of the subview . 如果他们触摸子视图并垂直于轴拖动,则将其视为子视图拖动

I posted some code that demonstrates this here - in an answer to a similar question . 我在此处发布了一些代码对此进行了演示,以解答类似问题

UIScrollView does not fire touch events methods for this you have to subclass UIScrollView. UIScrollView不会为此触发触摸事件方法,因此必须子类化UIScrollView。 Like below. 像下面。

@interface AppScrollView : UIScrollView {

}

- (id)initWithFrame:(CGRect)frame;

@end @结束

#import "AppScrollView.h"


@implementation AppScrollView

- (id)initWithFrame:(CGRect)frame 
 {
    if ([super initWithFrame:frame]){
    }
    return self;
   }

  - (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event 
  { 
  // If not dragging, send event to next responder
  if (!self.dragging) 
        [self.nextResponder touchesEnded: touches withEvent:event]; 
  else
     [super touchesEnded: touches withEvent: event];
 }


 @end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM