I use below code and work for 'self.view' but doesn't work for imageview and for direction just can add one direction how to use four direction(right,left,uo,down)?
UISwipeGestureRecognizer *swipe =[[UISwipeGestureRecognizer alloc]initWithTarget:selfaction:#selector(sr)];
swipe.direction =UISwipeGestureRecognizerDirectionRight;
[imageview addGestureRecognizer:swipe];
[swipe release];
Delegates
touchesBegan:
touchesEnded:
//--------add this code in the imageview allocation-------//
imageview.userInteractionEnabled = YES;
for four direction you have to use two UISwipeGestureRecognizer:-
UISwipeGestureRecognizer *swipeRightLeft =[[UISwipeGestureRecognizer alloc]initWithTarget:selfaction:#selector(sr)];
[swipeRightLeft setDirection:(UISwipeGestureRecognizerDirectionRight |UISwipeGestureRecognizerDirectionLeft )];
[imageview addGestureRecognizer:swipeRightLeft];
[swipeRightLeft release];
UISwipeGestureRecognizer *swipeUpDown =[[UISwipeGestureRecognizer alloc]initWithTarget:selfaction:#selector(sr)];
[swipeUpDown setDirection:(UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown )];
[imageview addGestureRecognizer:swipeUpDown];
[swipeUpDown release];
Try enabling interaction:
yourImageView.userInteractionEnabled = YES;
and add some directions, something like this:
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
If you are OK with your app only running on iOS 3.2+, the best way to do this is to use the UIGestureRecognizer API class. In fact, there is a UISwipeGestureRecognizer subclass (docs are here).
To do what you want, do something like this:
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(doSomething:)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[yourImageView addGestureRecognizer:swipe];
[swipe release];
To change to any other direction, look at the docs link above - there is a constant with all four directions.
Related
In viewDidLoad I set:
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[swipeGesture setDirection:(UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight)];
[self.view addGestureRecognizer:swipeGesture];
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"Swipe received.");
UISwipeGestureRecognizerDirection temp = recognizer.direction;
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft)
{
[self backCalendarPressed:nil];
}
else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
{
[self nextCalendarPressed:nil];
}
}
but recognizer.direction is always equal to '3'.
And that's why I can't determine if it's left or right swipe.
you have to set a separate gesture recognizer for each direction if you want to discern between left and right gestures. the direction property only gives you what you set as allowed direction (3 = both directions).
You can give the same target method to both, and in that method ask for the direction of the recognizer as you are doing.
You should try this. It is working fine.
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(backCalendarPressed:)];
[swipeLeft setDirection: UISwipeGestureRecognizerDirectionLeft ];
[self.view addGestureRecognizer:swipeLeft];
swipeLeft=nil;
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(nextCalendarPressed:)];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:swipeRight];
Basically you cannot set multiple directions to a single Swipe gesture recognizer. Use separate gesture recognizers for Left and Right directions.
try this:
The direction property only defines the allowed directions that are recognized as swipes, not the actual direction of a particular swipe.
The easiest would be to use two separate gesture recognizers instead.
If you want to capture swipes left and right that you can differentiate between, you'll have to set up a separate recognizer for each. Apple does this in their Simple Gesture Recognizers
UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFromLeft:)];
[swipeGestureLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:swipeGestureLeft];
UISwipeGestureRecognizer *swipeGestureRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFromRight:)];
[swipeGestureRight setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:swipeGestureRight];
-(void)handleSwipeFromLeft:(UISwipeGestureRecognizer *)recognizer {
[self backCalendarPressed:nil];
}
-(void)handleSwipeFromRight:(UISwipeGestureRecognizer *)recognizer {
[self nextCalendarPressed:nil];
}
You should implement all the UIGestureRecognizer states in this handler method. Thus you can handle all the cases. I am stating this because I suspect the gesture recognizer has not yet properly recognized it's gesture. Try looking for the direction in the UIGestureRecognizerStateChanged case.
Im Having MapView.. I added this as a subview of ViewController's View. I have the following Code in ViewDidLoad:
[self.view addSubview:mapView];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(mapLongPress:)];
longPressGesture.minimumPressDuration = 2;
[mapView addGestureRecognizer:longPressGesture];
[longPressGesture release];
And ,
- (void)mapLongPress:(UILongPressGestureRecognizer *)gestureRecognizer{
NSLog(#"Gesture");
if(gestureRecognizer.state == UIGestureRecognizerStateBegan){
CGPoint touchLocation = [gestureRecognizer locationInView:mapView];
CLLocationCoordinate2D coordinate;
coordinate = [mapView convertPoint:touchLocation toCoordinateFromView:mapView];
These i Got from StackOverFlow.. But Its not Working.. Did i need to do anything more in that?
Try adding [self.view addSubview:mapView]; after [mapView addGestureRecognizer:longPressGesture];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(mapLongPress:)];
longPressGesture.minimumPressDuration = 2;
longPressGesture.delegate = self;
[self.mapView addGestureRecognizer:longPressGesture];
[self.view addSubview:mapView];
[longPressGesture release];
Just add the UIGestureRecognizerDelegate to ViewController and do the above code given by me!!!
Try this:
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(mapLongPress:)];
lpgr.minimumPressDuration = 2.0;
[self.mapView addGestureRecognizer:lpgr];
This is using ARC, so I am not sure if you should release the gesture at this moment right now. Try it without releasing it, then try it with releasing it. See if that affects the gesture.
I have the following code which creates four gestures:
self.userInteractionEnabled = YES;
UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan:)];
[panGesture setDelegate:self];
[self addGestureRecognizer:panGesture];
UILongPressGestureRecognizer * longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:#selector(handleLongPress:)];
longPressGesture.minimumPressDuration = 0.00;
[self addGestureRecognizer:longPressGesture];
UISwipeGestureRecognizer * swipeUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(handleSwipeUp:)];
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
[self addGestureRecognizer:swipeUp];
UISwipeGestureRecognizer * swipeDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(handleSwipeDown:)];
swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
[self addGestureRecognizer:swipeDown];
The Pan and LongPress work fine, but I never get either of the Swipe gestures. Is there something special I need to do to have the swipe selectors get called?
Thanks
I just answered this yesterday.
Short form: a swipe gesture is a special case of a pan gesture, and by default no two gestures will recognize simultaneously. Look into gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: and/or requireGestureRecognizerToFail:. You'll find further help for this and related issues in Apple's guide.
I added this code in cellForRowAtIndexPath
UISwipeGestureRecognizer *gestureR = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSwipeFrom:)];
[gestureR setDirection:UISwipeGestureRecognizerDirectionRight];//|UISwipeGestureRecognizerDirectionRight)];
[cell addGestureRecognizer:gestureR];
it works fine. But I want UISwipeGestureRecognizerDirectionLeft so Added like this
[gestureR setDirection:UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight)];
When I check with direction and state I am always getting 3 = 3
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"%d = %d",recognizer.direction,recognizer.state);
}
if I apply only one Gesture it works fine. I tried to add two gestures one by one. but it will responding for only one gesture.
How to add second gestures. I added directly to one gesture to TableView another one to cell but now use.
Try this
UISwipeGestureRecognizer* gestureR;
gestureR = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom)] autorelease];
gestureR.direction = UISwipeGestureRecognizerDirectionLeft;
[view addGestureRecognizer:gestureR];
gestureR = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom)] autorelease];
gestureR.direction = UISwipeGestureRecognizerDirectionRight; // default
[view addGestureRecognizer:gestureR];
If you want to handle different functionalities on left and right swipes, just change the selectors.
Instead of two times alloc, it would be better if you use
UISwipeGestureRecognizer* recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
[recognizer setDirection:UISwipeGestureRecognizerDirectionLeft+UISwipeGestureRecognizerDirectionRight];
[cell addGestureRecognizer:recognizer];
And get the direction of swipe in the action as :
-(void)handleSwipe:(UISwipeGestureRecognizer *) sender
{
if (sender.direction == UISwipeGestureRecognizerDirectionLeft)
{
//do something
}
else //if (sender.direction == UISwipeGestureRecognizerDirectionRight)
{
//do something
}
}
I know its been ages since you asked this.
But try and read following line again in your question.
[gestureR setDirection:UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionRight)];
Did you realise you added UISwipeGestureRecognizerDirectionRight. Twice!!
:D
How to achieve touch even for UIImageView in appDelegate?
You can use:
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(anyMethod)];
[imageView addGestureRecognizer:tgr];
[tgr release];
Before doing this, enable user interaction of UIimageView.
This might be helpful for you:
http://mithin.in/2009/08/26/detecting-taps-and-events-on-uiwebview-the-right-way/