I have the following problem with a UIPanGestureRecognizer inside a UIScrollView:
UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(200, 200, 200, 200)];
sv.contentSize = CGSizeMake(200, 100 *100);
for (int i = 0; i < 100; i++) {
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, i * 100, 200, 100)];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panTile:)];
[panGesture setDelegate:self];
[panGesture setEnabled:FALSE];
[newView addGestureRecognizer:panGesture];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)];
[longPressRecognizer setDelegate:self];
[newView addGestureRecognizer:longPressRecognizer];
[sv addSubview:newView];
}
The complete scroll view is filled with small tiles, each of them implements a pan gesture in order to make them draggable. The problem is that - doing so - prevents the scroll view from scrolling. Dragging of the tiles instead works fine. When I disable the tiles pan gestures, scrolling works perfectly. The tiles pan gesture somewhat hides the scroll views own pan gesture. My idea was to disable the tiles pan gesture from the beginning. The gesture is enabled once the user does a long press on the tiles. The problem is that the user has to lift the finger and then touch the tile again to drag it. When the drag is finished, I enable the long press and disable the pan gesture again. So longPress: looks as follows:
- (void)longPress:(UILongPressGestureRecognizer *) gestureRecognizer {
for (UIGestureRecognizer *r in gestureRecognizer.view.gestureRecognizers) {
if ([r isKindOfClass:[UIPanGestureRecognizer class]]) {
[r setEnabled:TRUE];
}
}
//pan gesture should take over here...
}
Is there any possibility to glue the long press and the pan gestures together so that the user doesn't have to lift the finger? Or maybe another solution?
Related
I have an imageview and I want to tap on only one side of imageview. Is it possible to set frame for a gesture? Can anyone help with a solution?
Use UIGestureRecognizerDelegate, i think you can get the idea on how to compare:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if ([touch locationInView:yourview].x < somePoint.x) {
return false;
} else {
return true;
}
}
you could overlay a view on top of the imageview and add the tap recognizer to this new view, something like this will make the left hand side of the image tapable
UIView tapView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, imageView.frame.size.width/2, imageView.frame.size.height)];
[imageView addSubView:tapView]
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(handleSingleTap:)];
[tapView addGestureRecognizer:singleFingerTap];
You can simply put UIView on top of your imageView with frame as per your requirement and put tap gesture on that UIView.
You can do this by storyboard / xib or by programmatically.
By programmatically, for example - you want to tap only within the area with width of 50 px of your imageView. For this:
UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(imgView.frame.origin.x, imgView.frame.origin.y, 50, imgView.frame.size.height)];
// add gesture to view
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 2;
[vw addGestureRecognizer:tapGesture];
[imgView addSubview:vw];
now to handle your double tap
-(void)handleTapGesture:(UITapGestureRecognizer *)gesture
{
// handle double tap
}
I have a couple of UIScrollViews in my view controller. I want to overlay a view that captures a 2 finger swipe via UIPanGestureRecognizer which will not record the UIScrollView swipe gestures.
When I put a transparent view over my content with a 2 finger pan gesture, my taps and 1 finger swipes are not detected. I tried overwriting the pointInside: method to return NO
but then it doesn't record my 2 finger swipe.
The effect is similar to the 4 finger swipe to change apps.
You don't need an overlay view.
First implement UIPanGestureRecognizer that will handle 2 finger pan and assign it to your view that contains UIScrollViews
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:#selector(handlePan:)];
panGestureRecognizer.delegate = self;
panGestureRecognizer.minimumNumberOfTouches = 2;
panGestureRecognizer.maximumNumberOfTouches = 2;
[self.view addGestureRecognizer:panGestureRecognizer];
Use UIGestureRecognizerDelegate to handle 2 finger pan with UIScrollView pan gesture
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
And finally you are able to handle 2 fingers pan
- (void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer
{
NSLog(#"pan");
}
If you want to stop scrolling UIScrollView when two finger pan is detected you can disable and enable UIScrollView pan recognizers
- (void)handlePan:(UIPanGestureRecognizer *)gestureRecognizer
{
if(gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
_scrollView.panGestureRecognizer.enabled = NO;
}
if(gestureRecognizer.state == UIGestureRecognizerStateEnded)
{
_scrollView.panGestureRecognizer.enabled = YES;
}
NSLog(#"pan");
}
If you don't really need the overlay you can solve this with just gesture recognizers. I wrote this up as a test:
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
_scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 2, self.view.bounds.size.height);
UIView *green = [[UIView alloc] initWithFrame:self.view.bounds];
[green setBackgroundColor:[UIColor greenColor]];
UIView *blue = [[UIView alloc] initWithFrame:CGRectOffset(self.view.bounds, self.view.bounds.size.width, 0)];
[blue setBackgroundColor:[UIColor blueColor]];
[_scrollView addSubview:green];
[_scrollView addSubview:blue];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(twoFingerPan:)];
[pan setMinimumNumberOfTouches:2];
[pan setMaximumNumberOfTouches:2];
[pan setDelaysTouchesBegan:YES];
[_scrollView addGestureRecognizer:pan];
[self.view addSubview:_scrollView];
}
- (void)twoFingerPan:(UIPanGestureRecognizer *)gesture {
switch (gesture.state) {
case UIGestureRecognizerStateBegan:
self.scrollView.scrollEnabled = NO;
break;
case UIGestureRecognizerStateCancelled:
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateFailed:
self.scrollView.scrollEnabled = YES;
break;
default:
break;
}
NSLog(#"2 Fingers!");
}
I get the twoFingerPan: call back for when 2 fingers are used. The scroll view's panGestureRecognizer is still working at that point so I disable scrolling on the scroll view to handle the 2 finger pan. I've found this method work's pretty well. One sort of wonky thing is if the scroll view is decelerating the 2 finger gesture recognizer isn't called. Hope that helps!
So I have a method then when called generates a simple UIView with some labels in it:
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 300, 250)];
view1.backgroundColor = [UIColor redColor];
view1.userInteractionEnabled = YES;
[self.view addSubview:view1];
I call this method 6 times so it places 6 UIViews (I give them different coordinates of course) around the screen.
How can I detect when a user swipes right on one of them and then trigger some other method?
I've tried something like this:
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(myLabel1Tap)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[view1 addGestureRecognizer:swipeRight];
and then a method:
- (void)myLabel1Tap {
}
But I'm not sure what to do inside that method, how can I know which view was swiped if they are all called the same 'view1'?
change the gesture recognizers selector to accept an argument (by adding a colon after the method signature)
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(myLabel1Swipe:)];
this will mean the gesture recognizer will get passed in, and then you can perform actions based on the gesture recognizers properties, e.g.
- (void)myLabel1Swipe:(UISwipeGestureRecognizer *)recogniser
{
UIView *swipedView = [recognizer view];
//do whatever you want with this view
}
I have a UIViewController with a MKMapView and an other View (lets say PaintView) on top of the mapView.
When a user touches the PaintView with a pinch gesture, the mapview underneath should respond (as well as the paintView self).
When a user touches the Paintview with a pan gesture the mapview must not respond. (the pan gesture is used for painting).
Is there any possibility to forward the pinch gesture to the mapView?
[self.view addSubview:self.mapView];
[self.view addSubview:self.paintView];
UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(pan:)];
[self.paintView addGestureRecognizer:panRecognizer];
panRecognizer.delegate=self;
UIPinchGestureRecognizer* pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(pinch:)];
[self.paintView addGestureRecognizer:pinchGesture];
pinchGesture.delegate=self;
-(void)pan:(UIPanGestureRecognizer *)gesture
{
//do sth in the paintView
}
-(void)pinch:(UIPinchGestureRecognizer *)gesture
{
//forward pinch gesture to mapview
//do sth in paintview
}
I should implement a swipe gesture inside a subview; this subview is a oblique view
view1.transform = CGAffineTransformMakeRotation( ( 54 * -M_PI ) / 180 );
I want implement the swipe gesture inside this view, if it happens I should have a NSLog that say that swipe happens inside this view, is it possible?
Just as with any other UIView, it's possible to add a gesture recognizer:
UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipe:)];
[view1 addGestureRecognizer:swipeGestureRecognizer];
The problem lies in the UISwipeGestureRecognizer's property 'direction'. Apple documentation about this property:
The permitted direction of the swipe for this gesture recognizer.
Because the view is rotated, directions rotate along. If the view is rotated 180 degrees and the user swipes right, the gesture recognizer sees it as a left swipe. I'd suggest using a wrapper view on which the gesture recognizer should be placed. Try this:
UIView *view2 = [[UIView alloc] initWithFrame:view1.frame];
[view1.superview addSubview:view2];
view2.backgroundColor = [UIColor clearColor];
UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(didSwipe:)];
[view2 addGestureRecognizer:swipeGestureRecognizer];
The disadvantage is that there are certain areas within view2 but outside view1 which will respond to the gesture recognizer.