views:

4668

answers:

5

I use UIScrollView to make large-sized (larger than 320px) UI on iPhone.

I made an instance of UIScrollView and added some subviews on it. The problem is that I want to enable scrolling only when user touches outside of subviews, stop scrolling when user touches one of subviews.

I read documents and tried to find samples but I can't find good hint. If you have any idea, please help me.

+5  A: 

UIScrollView has a scrollEnabled property that allows you to disable scrolling programatically. It also has a delegate (UIScrollViewDelegate) that allows you to see events such as scrolling starting/ending. Seems that you should be able to cook something up with those options combined in some way.

Stephen Darlington
Thank you for your reply.I will try your solution now.
fish potato
This is the best solution, @Stephen: you are good :)
hungbm06
Nice answer, vague but enough to get started. +1
Yar
A: 

You can also sublcass UIScrollViewController and override the touchesBegan, touchesMoved and touchesEnded methods. If your implementation never calls the superclass implementation, then it won't scroll.

Airsource Ltd
Thank you for your answer.I tried your suggestion and it worked!!
fish potato
+3  A: 

If you want to detect touches inside any of the subviews of the UIScrollView, you will have to sublcass UIScrollView and override the touchesShouldBegin and touchesShouldCancelInContentView methods which are specifically created for this purpose.

Other than this, there is no way you can identify touches in the subviews as UIScrollView tends to handle all touches itself and doesn't pass them to its subviews.

All the best.

lostInTransit
Thank you for your answer.I tried to override touchesShouldBegin / touchesShouldCancel... methods but these methods were never called.Maybe something is wrong with my coding but I can stop scrolling by overriding touchesBegin/touchesMoved/touchedEnded methods.Thank you!
fish potato
Are you doing this in a subclass on UIScrollView? The methods will be called only then. So instead of your UIViewController using a UIView as its view it should use a subclass of UIScrollView as the view
lostInTransit
Yes, i tried this in a subclass of UIScrollView. I got some problem in my implement(only overriding touchesBegan...), so I keep trying to find the reason why touchesShouldBegin method was never called.
fish potato
A: 

The simplest way to do this is set delayContentTouches to NO for the scrollview. That way the default behaviour is set so that anything which is a UIControl will pass the touches on to the control immediately andeverything else will scroll.

Dave Verwer
If this is set to NO the behavior is the opposite of what the OP wants. If this is set to YES, it's not much better :)
Yar
A: 

The property you're really interested in -- and I'm actually testing this out right now, because I have the same problem you do -- is canCancelContentTouches.

If the value of this property is NO, the scroll view does not scroll regardless of finger movement once the content view starts tracking.

If this doesn't give you the results you want, subclass UIScrollView and override the touchesShouldBegin:withEvent:inContentView method, which is what the accepted answer suggests.

Yar