UIScrollView sending touches to subviews - iphone

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

Related

super becomeFirstResponder not working

I have a subview, and part of the view is transparent, so to the user, if they were to touch in that transparent space then whatever is visible (but also underneath it) should be interactive.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint pt = [[touches anyObject] locationInView:self];
if (CGRectContainsPoint(tableViewUse.frame, pt)){
[self becomeFirstResponder];
}else {
[super becomeFirstResponder];
}
}
I have the above code in my subview class, but the problem is that the [super becomeFirstResponder] call is not working. Now the obvious solution is to have a [self resignFirstResponder] call in the class of my superview; however, I plan to use many instances of this class in my code, so I would have to find the touch and compare it against the frame of each of my instances etc. So the elegant solution is to control everything from the subview.
Thank you for any help!
P.s just noticed a problem that will change my question. If I were to make the superview become the first responder, then any touchesBegan method will be called in that superview and the touch will have to be managed there. eeeeeek.
Tried this
[self.superview becomeFirstResponder]; ?

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

why does this touchesBegan method code get trigger in my UITableView?

Why does the touchesBegan method code get trigger in my UITableView?
Background: I have a UITableView within a UINavigationController. In the UITableView I have custom cells based on subclassing UITableViewCell. I current don't have a selectRowAtIndexPath method in my UITableView.
Some aspects of the answer that would be good to cover off would include:
why it doesn't work obviously
in terms of getting it to work, how does get one ensure that the correct touch (single-tap) detection will not prevent the other scenarios to work like: selecting a table row, scrolling the tableview contents up and down etc.
should the simulator be able to pick up touches etc, or are there cases here where you need the physical device
Code:
#interface AppointmentListController : UITableViewController <UIActionSheetDelegate>
.
.
.
#implementation AppointmentListController
.
.
.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
timeStampStart = event.timestamp;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSTimeInterval timeStampEnd = event.timestamp;
NSTimeInterval touchDuration = timeStampEnd - timeStampStart;
if(touchDuration > 0.2)
[super touchesEnded:touches withEvent:event];
else
DLog(#" - TOUCH EVENT OCCURS");
[super touchesEnded:touches withEvent:event];
}
Wanted to put up this as a possible answer, the answer being there is no relative straight way to achieve what I've asked for this. That is a way for a tap or double-tap to be detected in a UITableView page, which is already picking up row touches and scroll up/down etc.
Haven't verified whether this is the case or not, but people could up-vote this answer if they believe this to be true.

how to "bypass" touch events in Cocoa-touch?

I want to make a transparent mask-view over the current window, which just tracks touch events and passing them to the visible views below. However if I set userInteractionEnabled=YES to this mask, this blocks the events and won't be passed below.
Is there any way that I can prevent this view from blocking the events, or manually passing the events below?
Thanks,
I just recently did this for one of my apps and it turned out to be quite simple.
Get ready to subclass UIView:
I called my Mask View the catcher view and this is how the protocol looks:
#interface CatcherView : UIView {
UIView *viewBelow;
}
#property(nonatomic,retain)UIView *viewBelow;
#end
Here you are just subclassing UIView AND keeping a reference to the view bellow.
On the implementation you need to fully implement at least 4 methods to pass the touches to the view, or views bellow, this is how the methods look:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"Touch Began");
[self.viewBelow touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"Touch Moved");
[self.viewBelow touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"Touch Ended");
[self.viewBelow touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"Touch Cancelled");
//Not necessary for my app but you just need to forward it to your view bellow.
}
Just remember to set the view or views that are bellow when you create the view; it is also very important to set the background color to clear, so it acts as a mask. THis is how that looks:
CatcherView *catchView=[[CatcherView alloc] initWithFrame:[self.view bounds]];
catchView.backgroundColor=[UIColor clearColor];
catchView.viewBelow=myViewBellow;
[self.view addSubview:catchView];
Hope it helps and comment if you need more info.
UIKit determines the target view for an event by sending -hitTest:withEvent: messages down the responder chain
Once the target has been found, the event is sent up the responder chain until a responder that handles it is found (often the view that was touched, but not always)
Thus, if you override -[NSView hitTest:withEvent:] in a suitably high up view (perhaps by using a custom window!) you can note all incoming events and call super to have them behave as normal.

pass scroll event from uibutton to uiscrollview

i have horizontal UIScrollView which is extended from UIScrollView and i added UIButtons horizontally. i can only scroll out of the buttons area, but if i want to scroll over any buttons is fires UIControlEventTouchUpInside event. I don't want this. i want to fire UIControlEventTouchUpInside action if i click and i want to scroll if i scroll.
so how can i pass scroll event from UIButton to UIScrollView?
Subclass UIScrollView, return YES in the - touchesShouldCancelInContentView: method.
that functionality is already built in. When you have a UIControl element as a subview of a scroll view and a touch event is detected, it is initially passed to the UIScrollView. IF, after a moment or two there hasn't been sufficient movement in the touch event, it gets passed on to the button.
If you subclass NSButton and make your button of that type and override the following in your subclass you can pass the events back to the parent view, in your case the scroll view
The following would make the button never trigger an event and instead all would be dealt with by the parent view:
- (void)touchesBegan: (NSSet *)touches withEvent:(UIEvent *)event {
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesMoved: (NSSet *)touches withEvent:(UIEvent *)event {
[self.nextResponder touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event
{
[self.nextResponder touchesEnded:touches withEvent:event];
}
If you want to have the button deal with one of those events instead use
[super touchesEnded:touches withEvent:event];
Try this:
self.button.exclusiveTouch = YES
Worked like a charm for me!