简体   繁体   中英

Sending touch gestures to UIImageView subview of UIScrollView

I have programmed a UIImageView that allows me to draw inside of it. 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. 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.

How do I refer gestures made to the UIScrollView which are also inside of the UIImageView to the UIImageView.

I hope that makes sense

Best regards

EDIT: I have set all the following properties: canCancelContentTouches , exclusiveTouch and delaysContentTouches to 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

设置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:

self.scrollView.panGestureRecognizer.minimumNumberOfTouches = 2;

If you're targetting an earlier iOS, you can't use the panGestureRecognizer property. You have to dig through the scroll view's gestureRecognizers property to find the right recognizer. 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.

I don't favor two fingers, because I don't think it's a well known gesture on iOS. After one adds it, the next step is to add UI which explains it. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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