How to catch touchBegan by other subviews such us UIScrollView - iphone

I have a UIScrollView in my view control but my UIScrollView can't catch the touchBegan. touchBegan only execute when touching outside of the UIScrollView. How can catch the touchBegan when touching UIScrollView?
Thanks.

You must subclass UIScrollView (or an other view) and re-implement the methods you want to catch. Don't forget to call super in you implementation!

In order to subclass UIScrollView, your MyUIScrollView.h file should look similar to this:
#import <UIKit/UIKit.h>
#interface MyUIScrollView : UIScrollView {
}
#end
Then, in your MyUIScrollView.m, you place your touchesBegan method:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}
After you do this, anywhere in your code where you now use an UIScrollView, you switch to using a MyUIScrollView.

Related

UIScrollView sending touches to subviews

Note: I already read some questions about the UIScrollView sending touches to the subviews (this included and although I have up voted, it's not working as I intended anymore).
What I have: I have a UIScrollView with a Custom UIView (let's call it A) inside which covers the entire UIScrollView. I am also allowed to put other custom UIViews inside the A.
On the code I am doing this:
[scrollView setDelaysContentTouches:YES];
scrollView.canCancelContentTouches = NO;
What is happening: At the moment my only issue is that, if I want to move a subview inside A, I have to touch it, wait, and then move it. Exactly as stated here:
Now, the behaviour changes depending on the "length in time" of the
first touch on the UIView. If it's short, then the relative dragging
is managed as it was a scroll for the UIScrollView. If it's long, then
I'm getting the touchesMoved: events inside my UIView.
What I want: The subviews inside A should always receive priority and I shouldn't have to touch and wait. If I touch A and not a subview of it, I want the UIScrollView to receive the touches, like panning and moving around (the contentSize is bigger than the frame).
Edit 1.0
The only reason for me to have this A view inside a generic UIScrollView, is because I want to be able to zoom in/out on the A view. So I am doing the following:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return customView; // this is the A view
}
In the beginning I didn't had the A view inside the UIScrollView and the only thing I did was adding the A as a subView of my UIViewController's root view and everything went well. If there is another way to enable zoom in/out I will gladly accept the answer.
Note: Thank you all for your contributions, specially to Aaron Hayman.
I was able to figure it out by doing the following on the UIScrollView sub-class I had:
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
CGPoint pointOfContact = [gestureRecognizer locationInView:self];
// The view with a tag of 200 is my A view.
return (![[self hitTest:pointOfContact withEvent:nil] isEqual:[self viewWithTag:200]]);
}
I haven't tested this, but I believe how you are handling the touch events in View A (or it's subviews) will determine how touch events are passed on. Specifically, if you're trying to use the methods: touchesBegan, touchesMoves, touchesEnded, etc instead of a UIGestureRecognizer you won't receive the touches in the way you want. Apple design the UIGestureRecognizer to handle problems like the one you're facing. Specifically, the UIScrollView uses UIPanGestureRecognizer to handle the scrolling. If you add a UIPanGestureRecognizer to each of the subviews of View A any "panning" that occurs on one of those subviews should be sent to that subview instead of the UIScrollView. However, if you're simply using the "raw" touches methods, the UIPanGestureRecognizer in UIScrollView will never be cancelled.
In general, it's almost always best to use a UIGestureRecognizer instead of processing the touches directly in the view. If you need touches processed in a way that no standard UIGestureRecognizer can provide, subclass UIGestureRecognizer and process the touches there. That way you get all the the functionality of a UIGestureRecognizer along with your own custom touch processing. I really think Apple intended for UIGestureRecognizer to replace most (if not all) of the custom touch processing code that developers use on UIView. It allows for code-reuse and it's a lot easier to deal with when mitigating what code processes what touch event.
Jacky, I needed a similar thing: Within a building plan (your A, in my case a subclass of UIScrollView), let the user place and resize objects (call them Bs). Here's a sketch of what it took me to get at this behavior:
In the superview's (A) initWithFrame: method, set these two:
self.canCancelContentTouches = YES;
self.delaysContentTouches = NO;
This will ensure taps on B are immediately routed to the Bs.
In the embedded B, stop the superview A from cancelling taps, so it does not interfere with a gesture started on the B.
In the touchesBegan: method, search the view hierarchy upwards (using superview property of the views) until you find a UIScrollView, and set its canCancelContentTouches to NO. Remember the superview you changed, and restore this property in the touchesEnded and touchesCancelled methods of B.
I'd be interested whether this works for you as well. Good Luck!
nobi
I think you had better use "touchesBegan,touchesMoved,touchesEnded" to pass the event.
you can do like this:
you should make a mainView . It has 2 property. One is yourScrollView A , and One is yourCustomView.
`[yourScrollView addSubviews:yourCustomView];
[mainView addSubviews:yourScrollView];`
and then write your touches method in the mainView.m like this (ignor the scrollView statment)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
if ([[touches allObjects] isKindOfClass:[yourCustomView class]])
{
//do whatever you want
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
if ([[touches allObjects] isKindOfClass:[yourCustomView class]])
{
//do whatever you want
}
}
The last step: pass the event to the subview of the scrollView(your A).
#import "yourScrollView.h"
#implementation yourScrollView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
if(!self.dragging)
[[self nextResponder] touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesMoved:touches withEvent:event];
if(!self.dragging)
[[self nextResponder] touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesEnded:touches withEvent:event];
if(!self.dragging)
[[self nextResponder] touchesEnded:touches withEvent:event];
}
- (void)dealloc {
[super dealloc];
}
#end
wish to help you

Control UIScrollView outside bounds?

I'm wondering how i can allow the user to scroll outside the bounds of a UIScrollView?
You can try forwarding touch events from the various UIView methods of the superview to the scrollview, and see if that will work. E.g:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[scrollView touchesBegan:touches withEvent:event];
}
// etc
Or you might try using a UIPanGestureRecognizer on the superview and explicitly set the scroll view offset when you get the pan events. E.g:
- (void)handlePan:(UIPanGestureRecognizer *)pan
{
scrollView.contentOffset = [pan translationInView:scrollView];
}
// Or something like that.
Try subclassing the UIScrollView and overriding hitTest:withEvent: so that the UIScrollView picks up touches outside its bounds. Something like this:
#interface MagicScrollView : UIScrollView
#end
#implementation MagicScrollView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
// Intercept touches 100pt outside this view's bounds on all sides
if (CGRectContainsPoint(CGRectInset(self.bounds, -100, -100), point)) {
return self;
}
return nil;
}
#end
You may also need to override pointInside:withEvent: on the UIScrollView's superview, depending on your layout.
See the following question for more info: interaction beyond bounds of uiview

Method didn't work in UIScrollView

hello
i put some uitextfield in uiscrollview,and connect them in interface builder to event "touch down" to a method,the problem his that when the uitexttfield is inside uiscrollview the method doesn't called and when the textfield is not in the uiscroll view the method doesn't called.
UIScrollView is special because of all the touch events it needs to process itself. Try sub-classing UIScrollView and add:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"you should see this for touch events on your scroll view");
}
set the class name in IB to your UIScrollView subclass.

Passing scroll gesture to UIScrollView from another view

I do have a view that has a UIScrollView and over it there is a view that display some text.
When the user swipes over this view that contains text the UIScrollView won't scroll. How to make this view transparent in a way it relays the swipe gesture to UIScrollView.
Thanks
You can just set
myTextView.userInteractionEnabled = NO;
Or, if you're creating your view with Interface Builder, there's a check box there called 'User interaction enabled', just uncheck that.
Check out the UIView hitTest Method
Returns the farthest descendant of the receiver in the view hierarchy (including itself) that contains a specified point.
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
Inside the -touchesXXXX:withEvent: methods of your custom view, call their super methods to forward touch events.
For example:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// forward touchesBegan to super class
[super touchesBegan:touches withEvent:event];
...
// process touchesBegan for this view
}
Do the same things for touchesMoved, touchesEnded, and touchesCancelled.

UIScrollview+UItextview touch event

i am using a scroll view and that scroll view i have UIIMageview and UITextview.My Question is -i want the Touchevent of Textview.
I think UIScrollView becomes the first responder of touch events, and it doesn't pass the touch events to other view as a default.So, why don't you create a subclass of the UIScrollView? Then, pass the all touch events which are received by the subclass to the UIIMageview or UITextview like as follows.
#implementation SubclassOfScrollView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.nextResponder touchesBegan:touches withEvent:event];
}
#end