Irene
Irene

Reputation: 111

Why does touchesBegan stop working when UIImageView in placed inside a UIScrollView?

UIView -> UIImageView

I know I have things somewhat working ok since I can tap on my UIImageView and see an NSLog() statement in my touchesBegan method.

.

UIView -> UIScrollView -> UIImageView

I drag that same UIImageView into a UIScrollView and touchesBegan no longer gets called when I tap on my UIImageView. (I haven't changed anything else. All the same connections, methods, and code remains unchanged.)

Why does touchesBegan no longer work? And what can I do to get it working again?

Upvotes: 4

Views: 3051

Answers (3)

xhan
xhan

Reputation: 6287

by default UIImageView don't handle user gestures. set UIImageView instance's userInteractionEnabled to YES

Upvotes: 4

GameLoading
GameLoading

Reputation: 6708

Add uitapgesture to get event

Code is

  UITapGestureRecognizer *ges11=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Handeltap:)];
  [imagename addGestureRecognizer:ges11];

Create one action name "Handeltap" U will get called there.

Upvotes: 4

pwc
pwc

Reputation: 7103

Have a look at the documentation for UIScrollView.

Because a scroll view has no scroll bars, it must know whether a touch signals an intent to scroll versus an intent to track a subview in the content. To make this determination, it temporarily intercepts a touch-down event by starting a timer and, before the timer fires, seeing if the touching finger makes any movement. If the timer fires without a significant change in position, the scroll view sends tracking events to the touched subview of the content view. If the user then drags their finger far enough before the timer elapses, the scroll view cancels any tracking in the subview and performs the scrolling itself. Subclasses can override the touchesShouldBegin:withEvent:inContentView:, pagingEnabled, and touchesShouldCancelInContentView: methods (which are called by the scroll view) to affect how the scroll view handles scrolling gestures.

I'd also recommend reading the Scroll View Programming Guide.

Upvotes: 2

Related Questions