I am making a game where player moves with two figures at a time. Each one has its own half of the screen and moves just within in. Unfortunately I found out that when I swipe with both thumbs at a time nothing happens. Not even one of my recognizers are being triggered.
Maybe there is one way. I made another two views on the top of the GameViewController and added separate gestures. But I cant reffer to them in my gamescene.m to create actions.
Is there anyway to recognize swipes declared in GameViewController, in GameScene and add to them any actions?
I've already tried to make my own recognizers according to touch began and ended but when two fingers are being released at a time it got messy and usually forgot to react twice, I mean for each release separately.
-(void)setUpGestActions{
_swipeGestureLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeLeft:)];
[self.swipeGestureLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
self.swipeGestureLeft.cancelsTouchesInView = NO;
self.swipeGestureLeft.delegate = self;
[self.view addGestureRecognizer: self.swipeGestureLeft];
_swipeGestureRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeRight:)];
[self.swipeGestureRight setDirection:UISwipeGestureRecognizerDirectionRight];
self.swipeGestureRight.cancelsTouchesInView = NO;
self.swipeGestureRight.delegate = self;
[self.view addGestureRecognizer: self.swipeGestureRight];
_swipeGestureUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeUp:)];
[self.swipeGestureUp setDirection:UISwipeGestureRecognizerDirectionUp];
self.swipeGestureUp.cancelsTouchesInView = NO;
self.swipeGestureUp.delegate = self;
[self.view addGestureRecognizer: self.swipeGestureUp];
_swipeGestureDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeDown:)];
[self.swipeGestureDown setDirection:UISwipeGestureRecognizerDirectionDown];
self.swipeGestureDown.cancelsTouchesInView = NO;
self.swipeGestureDown.delegate = self;
[self.view addGestureRecognizer: self.swipeGestureDown];
_moveLeft = [SKAction moveByX:-self.frame.size.width/6 y:0 duration:self.velocity];
_moveRight = [SKAction moveByX:self.frame.size.width/6 y:0 duration:self.velocity];
_moveUp = [SKAction moveByX:0 y:self.frame.size.width/6 duration:self.velocity];
_moveDown = [SKAction moveByX:0 y:-self.frame.size.width/6 duration:self.velocity];
_downMovement = [SKAction moveByX:0 y:-1 duration:self.downMovementVelocity];
}
-(void)swipeLeft:(UISwipeGestureRecognizer*) recognizer{
_sideDisting = [recognizer locationInView:self.view];
if(self.sideDisting.x <= self.frame.size.width/2){
[_boy runAction:self.moveLeft];
}
else{
[_girl runAction:self.moveLeft];
}
}
-(void)swipeRight:(UISwipeGestureRecognizer*) recognizer{
_sideDisting = [recognizer locationInView:self.view];
if(self.sideDisting.x <= self.frame.size.width/2){
[_boy runAction:self.moveRight];
}
else{
[_girl runAction:self.moveRight];
}
}
-(void)swipeUp:(UISwipeGestureRecognizer*) recognizer{
_sideDisting = [recognizer locationInView:self.view];
if(self.sideDisting.x <= self.frame.size.width/2){
[_boy runAction:self.moveUp];
}
else{
[_girl runAction:self.moveUp];
}
}
-(void)swipeDown:(UISwipeGestureRecognizer*) recognizer{
_sideDisting = [recognizer locationInView:self.view];
if(self.sideDisting.x <= self.frame.size.width/2){
[_boy runAction:self.moveDown];
}
else{
[_girl runAction:self.moveDown];
}
}
To recognise multiple gestures at the same time, set a delegate on each gesture recogniser. The delegate can be the same object for each gesture.
In the delegate, implement this:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
I think you can look at this example.
It might help you.
UIScreenEdgePanGestureRecognizer *myScreenEdgePanGestureRecognizer;
...
myScreenEdgePanGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:#selector(handleScreenEdgePan:)];
myScreenEdgePanGestureRecognizer.delegate = self;
// Configure the gesture recognizer and attach it to the view.
...
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
BOOL result = NO;
if ((gestureRecognizer == myScreenEdgePanGestureRecognizer) && [[otherGestureRecognizer view] isDescendantOfView:[gestureRecognizer view]]) {
result = YES;
}
return result;
}
Go through this link, you will find more information.
https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizer_basics/GestureRecognizer_basics.html
The simplest solution is to divide screen into two minor views and attach separate gesture recognizers to each one.
Related
Following is the code I have written to put 2 finger swipe on UITableView :
UISwipeGestureRecognizer *leftSwipe = [UISwipeGestureRecognizer new];
[leftSwipe addTarget:self action:#selector(nextDay)];
leftSwipe.numberOfTouchesRequired = 2;
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
leftSwipe.delegate = self;
[leftSwipe setCancelsTouchesInView:YES];
[tableViewTasks addGestureRecognizer:leftSwipe];
UISwipeGestureRecognizer *rightSwipe = [UISwipeGestureRecognizer new];
[rightSwipe addTarget:self action:#selector(previousDay)];
rightSwipe.numberOfTouchesRequired = 2;
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
rightSwipe.delegate = self;
[rightSwipe setCancelsTouchesInView:YES];
[tableViewTasks addGestureRecognizer:rightSwipe];
I am using SWTableViewCell which has left and right (single tap) gestureRecognisers.
When UITableView is swiped left/right using 2 fingers, SWTableViewCell left and right gestures are also fired after that.
How to stop the conflict ?
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
if (SWTableViewCellTouch) {
SWTableViewCellTouch = NO
return NO;
}
return YES;
}
when you touch the SWTableViewCell set a BOOL SWTableViewCellTouch to YES.
The possible solution is enable/disable the BOOl (SWTableViewCellTouch) in the touchesBegan: method as below.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([[event touchesForView:self] count] > 1) {
// Its two finger touch so set the BOOL false like
SWTableViewCellTouch = NO;
}
else if ([[event touchesForView:self] count] == 1){
// Its sigle finger touch so set the BOOL true like
SWTableViewCellTouch = YES;
}
[super touchesBegan:touches withEvent:event] ;}
Hope this will help you.
1. Implement UIGestureRecognizerDelegate in your UIViewController
2. Set leftSwipe.delegate = self; and leftSwipe.delegate = self;
3. Now check if the in its Delegate Method if UISwipeGesture have how many numberOfTouchesRequired
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ( [gestureRecognizer isMemberOfClass:[UISwipeGestureRecognizer class]] ) {
UISwipeGestureRecognizer *swipeGesture=(UISwipeGestureRecognizer *)gestureRecognizer ;
if(swipeGesture.numberOfTouchesRequired!=2)
{
//if Double not Double Swipe Touch Don't Linsten Gesture in your Viewcontroller
return NO;
}
}
return YES;
}
i hope this will solve your problem
I am working on an application which works like a remote control of different devices like Game Console, Set-top Box, etc.
Application contains different type of controls like gesture and buttons to perform events as remote.
Single gesture view contains multiple gesture recognizer as follow:
1 finger Tap using UITapGestureRecognizer
2 finger Tap using UITapGestureRecognizer
1 finger swipe using UISwipeGestureRecognizer
2 finger swipe using UISwipeGestureRecognizer
My requirement is to implement 1 finger Swipe gesture recognizer along with hold event. So, that will continue my swipe action until user will not remove its finger from the view.
I have tried UILongPressGestureRecognizer after UISwipeGestureRecognizer but this didn’t work for me because it is executing along with Swipe movement. And I want this after Swipe end, but finger will not remove.
My code snippet :
-(void) gestureView:(UIView*)viewGesture {
UISwipeGestureRecognizer *swipeGestureSingleRight;
UISwipeGestureRecognizer *swipeGestureSingleLeft;
UISwipeGestureRecognizer *swipeGestureSingleUp;
UISwipeGestureRecognizer *swipeGestureSingleDown;
swipeGestureSingleUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeGestureSingle_Handle:)];
[swipeGestureSingleUp setDelegate:self];
[swipeGestureSingleUp setDirection:(UISwipeGestureRecognizerDirectionUp)];
[swipeGestureSingleUp setNumberOfTouchesRequired:1];
[viewGesture addGestureRecognizer:swipeGestureSingleUp];
swipeGestureSingleDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeGestureSingle_Handle:)];
[swipeGestureSingleDown setDelegate:self];
[swipeGestureSingleDown setDirection:(UISwipeGestureRecognizerDirectionDown)];
[swipeGestureSingleDown setNumberOfTouchesRequired:1];
[viewGesture addGestureRecognizer:swipeGestureSingleDown];
swipeGestureSingleRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeGestureSingle_Handle:)];
[swipeGestureSingleRight setDelegate:self];
[swipeGestureSingleRight setDirection:(UISwipeGestureRecognizerDirectionRight)];
[swipeGestureSingleRight setNumberOfTouchesRequired:1];
[viewGesture addGestureRecognizer:swipeGestureSingleRight];
swipeGestureSingleLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeGestureSingle_Handle:)];
[swipeGestureSingleLeft setDelegate:self];
[swipeGestureSingleLeft setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[swipeGestureSingleLeft setNumberOfTouchesRequired:1];
[viewGesture addGestureRecognizer:swipeGestureSingleLeft];
longPressGestureOnSwipe = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressGesture_Handle:)];
longPressGestureOnSwipe.minimumPressDuration = 0.03;
longPressGestureOnSwipe.delegate = self;
[viewGesture addGestureRecognizer:longPressGestureOnSwipe];
isSwipeEnd = false;
[longPressGestureOnSwipe setEnabled:isSwipeEnd];
}
- (void)swipeGestureSingle_Handle:(UISwipeGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
singleTouchSwipeSenderDirection = sender.direction;
isSwipeEnd = true;
[longPressGestureOnSwipe setEnabled:isSwipeEnd];
}
}
-(void)longPressGesture_Handle:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
isSwipeEnd = false;
[longPressGestureOnSwipe setEnabled:isSwipeEnd];
}
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
if (isSwipeEnd) {
[super touchesBegan:touches withEvent:event];
} else {
return;
}
}
Please suggest me correction or solution for my requirement.
Thanks in advance!
I have been attempting to figure out how I can associate one UISwipeGestureRecognizer with the right half of the screen and another UISwipeGesture recognizer with the other half of the screen, however, I have been unsuccessful in coding this mechanic properly. Below is my current code. I have no idea on how I can associate one of the swipe recognizers with one half of the screen. Any help would be much appreciated
-(void)didMoveToView:(SKView *)view {
UISwipeGestureRecognizer *leftSwipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwiped1)];
[leftSwipe1 setDirection:UISwipeGestureRecognizerDirectionLeft];
[leftSwipe1 setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:leftSwipe1];
UISwipeGestureRecognizer *rightSwipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwiped1)];
[rightSwipe1 setDirection:UISwipeGestureRecognizerDirectionRight];
[rightSwipe1 setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:rightSwipe1];
self.physicsWorld.gravity = CGVectorMake(0, -9.8);
self.physicsWorld.contactDelegate = self;
}
-(void)rightSwiped1 {
SKNode *person1 = [self childNodeWithName:#"person1"];
SKAction *moveRight = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) - 80, CGRectGetMidY(self.frame) + 200) duration:0.2f];
[person1 runAction:moveRight];
}
-(void)leftSwiped1 {
SKNode *person1 = [self childNodeWithName:#"person1"];
SKAction *moveLeft = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) - 400, CGRectGetMidY(self.frame) + 200) duration:0.2f];
[person1 runAction:moveLeft];
}
In a nutshell; You will have to do the filtering yourself.
In the documentation for UISwipeGestureRecognizer it states:
You may determine the location where a swipe began by calling the
UIGestureRecognizer methods locationInView: and
locationOfTouch:inView:. The former method gives you the centroid if
more than one touch was involved in the gesture; the latter gives the
location of a particular touch.
For example: For the left swipe gesture you will do something like
First change your initWithTarget action selector to take an argument like this
UISwipeGestureRecognizer *leftSwipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwiped1:)];
Then in your handler do something like this:
-(void)leftSwiped1:(UIGestureRecognizer *)gestureRecognizer {
CGPoint pt = [gestureRecognizer locationInView:self.view];
if(pt.x < (self.view.bounds.size.width/2))
{
SKNode *person1 = [self childNodeWithName:#"person1"];
SKAction *moveLeft = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) - 400, CGRectGetMidY(self.frame) + 200) duration:0.2f];
[person1 runAction:moveLeft];
}
}
I've set up my UIView to call a method when the view is panned up and to call some other method when the view is swiped up. My pan works fine and I can keep my pan disabled if my velocity.y is above a certain limit, but I can never get the swipe action to work when my pan fails. I've tried playing around with the delegate methods without much luck. Looked over this solution but no luck: https://stackoverflow.com/a/21728621/1925859
- (void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer * panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
panRec.delegate = self;
[panedView addGestureRecognizer:panRec];
UISwipeGestureRecognizer * swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
swipeRec.delegate = self;
[panedView addGestureRecognizer:swipeRec];
[swipeRec requireGestureRecognizerToFail:panRec];
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
{
UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gestureRecognizer;
CGPoint velocity = [pan velocityInView:gestureRecognizer.view];
if (ABS(velocity.y) > 200)
{
NSLog(#"Swipe actiavted");
return NO;
}
}
return YES;
}
- (IBAction)handleSwipe:(UISwipeGestureRecognizer *)recognizer
{
NSLog(#"In Swipe");
if(recognizer.direction == UISwipeGestureRecognizerDirectionUp)
{
panedView.frame =CGRectOffset( panedView.frame, 0, -1*self.view.bounds.size.height*.80);
}
}
If you remove your delegate method, add direction to the swipe and change the receiver of requireGestureRecognizerToFail then it will work. Note that pans must be way slower than sweeps in order to be recognized.
- (void)viewDidLoad
{
[super viewDidLoad];
UIPanGestureRecognizer * panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
[panedView addGestureRecognizer:panRec];
UISwipeGestureRecognizer * swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
swipeRec.direction = UISwipeGestureRecognizerDirectionUp;
[panedView addGestureRecognizer:swipeRec];
[panRec requireGestureRecognizerToFail:swipeRec];
}
- (IBAction)handleSwipe:(UISwipeGestureRecognizer *)recognizer
{
NSLog(#"In Swipe");
if(recognizer.direction == UISwipeGestureRecognizerDirectionUp)
{
panedView.frame =CGRectOffset( panedView.frame, 0, -1*self.view.bounds.size.height*.80);
}
}
- (IBAction)handlePan:(UISwipeGestureRecognizer *)recognizer
{
NSLog(#"PAN");
}
I have a cutsom UITableViewCell implemention.
I have registered this subclass of UITableViewCell for a UIPanGestureRecognizer which i use to swiping the cells to the right or left.
// in the UITableViewCell subclass :
UIGestureRecognizer* recognizer =
[[UIPanGestureRecognizer alloc] initWithTarget:
self
action:#selector(handlePan:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];
recognizer.cancelsTouchesInView = NO;
Now I want to to present a view controller when the user does a two finger swipe "up"
on the screen.
So, I added a UISwipeGestureRecognizer to the tableview.
// code in the view controller containing the tableview reference.
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleViewsSwipe:)];
[swipe setDirection:UISwipeGestureRecognizerDirectionUp];
[swipe setDelaysTouchesBegan:NO];
[[self tableView ]addGestureRecognizer:swipe];
swipe.cancelsTouchesInView= YES;
[swipe setNumberOfTouchesRequired:2];
swipe.delegate = self;
self.tableView.multipleTouchEnabled = YES;
But when I do a two finger swipe on the screen, the pan gesture gets triggered .
How can I solve this ?
As sooper's says, setting the maximumNumberOfTouches = 1 will probably work.
For others trying to deal with 2 gestureRecognizers at the same time that are both 1 touch gestures I found that making sure to set this delegate to yes
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
and then in the gesture recognizer action you can check for a certain translation or whatever you need and cancel one of the gesture recognizers.
Such as:
- (void)panSwipeRecognizer:(UIPanGestureRecognizer*)panRecognizer
{
CGPoint translation = [panRecognizer translationInView:self.superview];
if(panRecognizer.state == UIGestureRecognizerStateBegan)
{
if(fabsf(translation.x) < fabsf(translation.y))
{
//deactivate horizontal gesture recognizer
panRecognizer.enabled = NO;
panRecognizer.enabled = YES;
}
else //if(fabsf(translation.x) > fabsf(translation.y))
{
//deactivate vertical gesture recognizer
otherGestureRecognizer.enabled = NO;
otherGestureRecognizer.enabled = YES;
}
}
//other statements like stateChanged and stateBegan
}