I have already tried by this code but gesture not detect.
UISwipeGestureRecognizer *mSwipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(up_downBtn_waytosalon)];
[mSwipeUpRecognizer setDirection:UISwipeGestureRecognizerDirectionUp];
[self.duration_distanceView addGestureRecognizer:mSwipeUpRecognizer];
First Off, you need to always pass the gesture recognizer to your selector, otherwise you can't determine the state of your gesture.
Other than that first check if that view has userInteractionEnabled to YES, I'm guessing that view doesn't get any user interaction.
YOUR duration_distanceView view is imageView then add below line your code.
self.duration_distanceView.userInteractionEnabled = YES;
Try this code :
UISwipeGestureRecognizer *mSwipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(up_downBtn_waytosalon)];
self.duration_distanceView.userInteractionEnabled = YES;
[mSwipeUpRecognizer setDirection:UISwipeGestureRecognizerDirectionUp];
[self.duration_distanceView addGestureRecognizer:mSwipeUpRecognizer];
Initialize swipe gesture like this:
UISwipeGestureRecognizer *mSwipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(up_downBtn_waytosalon:)];
mSwipeUpRecognizer.direction = (UISwipeGestureRecognizerDirectionUp);
[self.duration_distanceView addGestureRecognizer:mSwipeUpRecognizer];
Implement the gesture action like this:
- (void) up_downBtn_waytosalon:(UISwipeGestureRecognizer *)sender {
if ( sender.direction | UISwipeGestureRecognizerDirectionUp )
NSLog(#"Its swipe up");
}
Hope this code helps.
Related
I'm trying to detecting a two finger swipe touch and use it like the touch board in Mac(two finger up and two finger down listening) and the code like;
UISwipeGestureRecognizer *swipeUpTwoFinger = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeTwoFinger:)];
swipeUpTwoFinger.numberOfTouchesRequired = 2;
swipeUpTwoFinger.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeUpTwoFinger];
UISwipeGestureRecognizer *swipeDownTwoFinger = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeTwoFinger:)];
swipeDownTwoFinger.numberOfTouchesRequired = 2;
swipeDownTwoFinger.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipeDownTwoFinger];
-(void)handleSwipeTwoFinger:(UISwipeGestureRecognizer *)swipeGestureRecognizer
{
if (swipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionUp) {
NSLog(#"handleSwipeUpTwoFinger");
}
else if (swipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionDown) {
NSLog(#"handleSwipeDownTwoFinger");
}
}
The Problem is
I could get a Up or Down Swipe when I touch,but just one event whatever I do the Up Down swipe under one pressing.
What I want is like the touch board in Mac,when I swipe up it listened and without leaving the board swipe down it could listened too.
The Question is
How could I do this with UISwipeGestureRecognizer?
Or it have limitation couldn't do what I want should I do it with UIPanGestureRecognizer or listening various touch event do logic with myself? or any other implementation ? any demo?
any suggestions would be appreciated :)
I'm working on a GLKViewController based game which interprets taps and swipes as game controls. I want to support two-player mode by letting the first player tap or swipe on the left side of the screen, and letting the second player tap or swipe on the right side of the screen. In a perfect world, I'd like the gesture recognizers to work even if the swipes are sloppy and go past the centerline of the screen (with the starting point of the swipe being used to determine which player gets the input).
What would be the best way to implement this? Can I lay down a gesture recognizer on the left half of the screen, and another one on the right side of the screen? Will two separate recognizers work properly together even if both sides are being tapped/swiped rapidly at the same time? Or should I create a full-screen recognizer and parse the swipes and taps entirely on my own? I don't have experience with gesture recognizers so I don't know what the preferred approach is or how well they work when you have more than one being swiped on simultaneously.
I ended up making two UIViews overlaid on top of my GLKView, one on the left side of the screen and one on the right. Each view has a UIPanGestureRecognizer and a UILongPressGestureRecognizer (the long-press recognizer is basically a more flexible tap—I needed to use it to reject some gestures from being interpreted both as a pan and a tap at the same time). This worked magnificantly.
- (void)viewDidLoad
{
[super viewDidLoad];
// Add tap and pan gesture recognizers to handle game input.
{
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handleLeftSidePan:)];
panRecognizer.delegate = self;
[self.leftSideView addGestureRecognizer:panRecognizer];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleLeftSideLongPress:)];
longPressRecognizer.delegate = self;
longPressRecognizer.minimumPressDuration = 0.0;
[self.leftSideView addGestureRecognizer:longPressRecognizer];
}
{
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handleRightSidePan:)];
panRecognizer.delegate = self;
[self.rightSideView addGestureRecognizer:panRecognizer];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(handleRightSideLongPress:)];
longPressRecognizer.delegate = self;
longPressRecognizer.minimumPressDuration = 0.0;
[self.rightSideView addGestureRecognizer:longPressRecognizer];
}
}
- (void)handleLeftSidePan:(UIPanGestureRecognizer *)panRecognizer
{
[self handleGameScreenPan:panRecognizer withVirtualController:&g_iPadVirtualController[0]];
}
- (void)handleRightSidePan:(UIPanGestureRecognizer *)panRecognizer
{
[self handleGameScreenPan:panRecognizer withVirtualController:&g_iPadVirtualController[1]];
}
- (void)handleLeftSideLongPress:(UILongPressGestureRecognizer *)longPressRecognizer
{
[self handleGameScreenLongPress:longPressRecognizer withVirtualController:&g_iPadVirtualController[0]];
}
- (void)handleRightSideLongPress:(UILongPressGestureRecognizer *)longPressRecognizer
{
[self handleGameScreenLongPress:longPressRecognizer withVirtualController:&g_iPadVirtualController[1]];
}
I followed that tutorial to implement the basic setup of the ECSLidingViewController:
http://www.youtube.com/watch?v=tJJMyzdB9uI
Now I want to change the transition of opening the leftView like the Default Example of the TransitionFun example.
I added the following code to the leftView:
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(closeSettings)];
[rightRecognizer setDirection:UISwipeGestureRecognizerDirectionRight];
[self.slidingViewController.topViewController.view addGestureRecognizer:rightRecognizer];
Additionaly I added the line
[self.slidingViewController.topViewController.view addGestureRecognizer:self.slidingViewController.panGesture];
After the first start of the app, the topview is following my fingers while opening/closing. But after choosing one point of the leftview tableview, it's not working anymore.
In all my topViews, I have the following code in the viewDidLoad method:
UIScreenEdgePanGestureRecognizer *leftRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:#selector(openSettings:)];
leftRecognizer.edges = UIRectEdgeLeft;
[self.view addGestureRecognizer:leftRecognizer];
Can anybody tell me what to change, that the behaviour of the first start is all the time?
sorry that was a stupid mistake:
I forgot to add
[self.view addGestureRecognizer:self.slidingViewController.panGesture];
to each topViewController. Now it's working.
I'm writing code to move my two fingers up or down on a view to change some status. The code as below:
UISwipeGestureRecognizer *aSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeGesture:)];
[aSwipeGesture setDirection:UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown];
aSwipeGesture.numberOfTouchesRequired = 2;
[self.View addGestureRecognizer:aSwipeGesture];
- (void)swipeGesture:(UISwipeGestureRecognizer *)sender {
NSLog(#"Swipe received.");
if (sender.direction==UISwipeGestureRecognizerDirectionUp) {
NSLog(#"swipe up");
} else if (sender.direction==UISwipeGestureRecognizerDirectionDown) {
NSLog(#"swipe down");
}
}
However the only print log I could receive was Swipe received as below shows. I couldn't get the message for swipe up or swipe down, did I miss anything? Thanks
ViewController.m:228 Swipe received.
ViewController.m:228 Swipe received.
ViewController.m:228 Swipe received.
ViewController.m:228 Swipe received.
ViewController.m:228 Swipe received.
ViewController.m:228 Swipe received.
Updated: I have to use two fingers to finish the swipe action.
Try this
UISwipeGestureRecognizer *aSwipeGestureUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeGesture:)];
[aSwipeGesture setDirection:UISwipeGestureRecognizerDirectionUp];
aSwipeGestureUp.numberOfTouchesRequired = 2;
[self.View addGestureRecognizer:aSwipeGestureUp];
UISwipeGestureRecognizer *aSwipeGestureDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeGesture:)];
[aSwipeGestureDown setDirection:UISwipeGestureRecognizerDirectionDown];
aSwipeGestureDown.numberOfTouchesRequired = 2;
[self.View addGestureRecognizer:aSwipeGestureDown];
sadly the API makes no sense at all, you can SET a UISwipeGestureRecognizer to recognize a swipe in any of the 4 directions, but it will not tell you what direction the swipe happened in.
they were careful enough to allow the 4 constants to be used together (it's a bitmap) but the API is just broken.
you will have to use one recognizer for each direction, a shame !
In your case you have set Direction of swipe as UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown . So in delegate callback you will receive same value for sender.direction, that is why the logs are not printed as the enum value is not matching with sender.direction value.
If you want to handle separately the up and down swipe, you need to create 2 swipe gestures with up and down swipe directions and add to your view. Then based on the direction of sender do the task.
UISwipeGestureRecognizer *Updown=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(handleGestureNext:)];
Updown.delegate=self;
[Updown setDirection:UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionUp];
[overLayView addGestureRecognizer:Updown];
UISwipeGestureRecognizer *LeftRight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(handleGestureNext:)];
LeftRight.delegate=self;
[LeftRight setDirection:UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight];
[overLayView addGestureRecognizer:LeftRight];
overLayView.userInteractionEnabled=NO;
-(void)handleGestureNext:(UISwipeGestureRecognizer *)recognizer
{
NSLog(#"Swipe Recevied");
//Left
//Right
//Top
//Bottom
}
remove aSwipeGesture.numberOfTouchesRequired = 2; and try
In my app I have added the new Gesture Recognizers that are available in the 3.2 SDK. Everything appears to be working correctly and the response time on the screen been very fast. But for some reason when I add requireGestureRecognizerToFail to some of my gestures, there is a very visible delay when the gesture is triggered. Below is a snippet of the code that I use to create the Gesture Recognizers. Does anyone know why there is a delay and how I can fix it? I'm using requireGestureRecognizerToFail to prevent the SingleTap gesture from triggering when the user performs a DoubleTap.
- (void)createGestureRecognizers {
//Single Finger Double-Tap
UITapGestureRecognizer *singleFingerDTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleDoubleTap:)];
singleFingerDTap.numberOfTapsRequired = 2;
[super addGestureRecognizer:singleFingerDTap];
//Single Finger Tap
UITapGestureRecognizer *singleFingerTap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleSingleTap:)];
singleFingerTap.numberOfTapsRequired = 1;
[singleFingerTap requireGestureRecognizerToFail:singleFingerDTap];
[self addGestureRecognizer:singleFingerTap];
//Two Finger Pan
UIPanGestureRecognizer *panGesture2 = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePanGesture2:)];
panGesture2.maximumNumberOfTouches = 2;
[super addGestureRecognizer:panGesture2];
//Single Finger Pan
UIPanGestureRecognizer *panGesture1 = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:#selector(handlePanGesture1:)];
panGesture1.maximumNumberOfTouches = 1;
[panGesture1 requireGestureRecognizerToFail:panGesture2];
[super addGestureRecognizer:panGesture1];
[singleFingerDTap release];
[singleFingerTap release];
[panGesture1 release];
[panGesture2 release];
}
If you want to distinguish between a single and double tap, you must wait long enough to figure out that no second tap is coming before you can call it a single tap. The alternative would be to design all your single tap actions in such a way that they can asynchronously be canceled or reverted when a double tap is detected.
For example, if you have a single tap change pages and a double tap zoom, then you would have to animate a page changing on single tap, then reverse the animation and zoom instead when a second tap is detected. By then the view that handled the single tap may have moved. In most cases, that is more trouble and confusion then it is worth.