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.
Related
I have tried so many thing to get touch inside a UIScrollView but I'm not getting how to do it.
Actually I want to touch inside a UIScrollView and get the location of subview (which i have touched).
You can do it with gesture recognizers. For detect single tap location use UITapGestureRecognizer
UITapGestureRecognizer *tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction:)] autorelease];
[myScrollView addGestureRecognizer:tapRecognizer];
- (void)tapAction:(UITapGestureRecognizer*)sender{
CGPoint tapPoint = [sender locationInView:myScrollView];
CGPoint tapPointInView = [myScrollView convertPoint:tapPoint toView:self.view];
}
To convert that tapPoint to self.view you can use convertPoint:toView: method in UIView class
How to add a pan gesture to first half (0,0,160,480) of a view and on same view a swipe gesture on (160,0,160,480) as well?
The view is table view and on swiping left the cell content should change and on panning in right the tableview should move like facebook ios app
Take two Different UIview as SubView of mainView with Required Frame Size then add Desired Gestures :-
UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeVC:)];
swipeGestureRecognizer.delegate = self;
[self.subView.view addGestureRecognizer:swipeGestureRecognizer];
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panVC:)];
panGestureRecognizer.delegate = self;
[panGestureRecognizer requireGestureRecognizerToFail:swipeGestureRecognizer];
[self.subView2.view addGestureRecognizer:panGestureRecognizer];
The Below method is called when recognition of a gesture by either gestureRecognizer or otherGestureRecognizer
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
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 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?