Reputation: 4833
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
Upvotes: 2
Views: 4348
Reputation: 2285
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
Upvotes: 0
Reputation: 62676
@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.
Upvotes: 0
Reputation: 385600
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.
Upvotes: 0
Reputation: 19882
Set the exclusiveTouch
property of the UIImageView to YES
(This means that, when the UIImageView is touched, that touch will not have effects on any other views)
Upvotes: 1