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.
Related
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 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.
I am trying to rotate the custom view added to the main view. I have tried to add the gesture recognizer to both main view and sub view. Here is my code
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *demoView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
UIRotationGestureRecognizer *rg= [[UIRotationGestureRecognizer alloc]initWithTarget:self action:#selector(handleTap:)];
[rg setDelegate:self];
self.view.userInteractionEnabled = YES;
demoView.userInteractionEnabled = YES;
[demoView addGestureRecognizer:rg];
[self.view addGestureRecognizer:rg];
demoView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:demoView];
}
-(IBAction)handleTap:(UIRotationGestureRecognizer *)recognizer
{
//NSLog(#"HERE");
recognizer.view.transform = CGAffineTransformMakeRotation([recognizer rotation]);
recognizer.rotation = 0;
//NSLog(#"AND HERE");
}
the problem is that one geturerecognizer can only ever be attached to one view. for your requirements, you need two gesturerecognizers, but you can make them call the same selector, so theres no need to implement it two times.
EDIT:
Its kinda hard to find this stated clearly in the docs, but considering that the view- property is a single view, and not an array or set or whatever, it makes alot of sense.
Heres the docs:
https://developer.apple.com/library/ios/documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html#//apple_ref/occ/instp/UIGestureRecognizer/view
Next EDIT
This stackoverflow-answer also illustrates my point:
Can you attach a UIGestureRecognizer to multiple views?
so whoever downvoted my answer, at least tell me why.
I've been trying to use a UILongPressGestureRecognizer in a MKAnnotationView subclass. Interestingly the gesture recognizer only triggers when using two ore fingers/touches.
What prevents the gesture recognizer to get triggered with only one touch?
Implementation
UILongPressGestureRecognizer *pressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self
action:#selector(handleLongPress:)];
pressRecognizer.minimumPressDuration = 0.25;
pressRecognizer.numberOfTapsRequired = 0;
pressRecognizer.numberOfTouchesRequired = 1;
The same implementation in a normal UIView shows the expected behaviour working with one touch. Yet it's possible to use touchesBegan: and touchesEnded: to get a long press gesture working I'm still curious what the reason for this is.
Have you seen this question ?
For using my UILongPressGestureRecognizer I disabled AnnotationView and added GestureRecognizer to it:
[ann_view setEnabled:NO];
UILongPressGestureRecognizer* long_press = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressAnnotationView:)];
long_press.minimumPressDuration = 1.5;
[ann_view addGestureRecognizer:long_press];
[long_press release];
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.