how do I associate a UISwipeGestureRecognizer with one side of the screen? - ios

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];
}
}

Related

Simulatenous independent gestures

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.

Problems with Swipe Gesture in Landscape Mode

I have been trying to figure out a way to fix this on my own, without any luck.
Programming Language is Objective-C and SpriteKit is the technology.
I'm trying to make an SKNode move around the screen using swipe gestures. (In a landscape mode app)
The problem is when I swipe left or any other sides, the node will move the opposite direction and use another method than it's supposed to. It looks like the iPhone thinks it is in portrait mode. What should I do?
Here's the code:
Inside .h Declaring the node
SKNode *Main_Character;
Inside .m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
UISwipeGestureRecognizer *oneFingerSwipeLeft = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(SwipedLeft)] ;
[oneFingerSwipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:oneFingerSwipeLeft];
UISwipeGestureRecognizer *oneFingerSwipeRight = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(SwipedRight)] ;
[oneFingerSwipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:oneFingerSwipeRight];
UISwipeGestureRecognizer *oneFingerSwipeUp = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(SwipedUp)] ;
[oneFingerSwipeLeft setDirection:UISwipeGestureRecognizerDirectionUp];
[[self view] addGestureRecognizer:oneFingerSwipeUp];
UISwipeGestureRecognizer *oneFingerSwipeDown = [[UISwipeGestureRecognizer alloc]
initWithTarget:self
action:#selector(SwipedDown)] ;
[oneFingerSwipeLeft setDirection:UISwipeGestureRecognizerDirectionDown];
[[self view] addGestureRecognizer:oneFingerSwipeDown];
}
// Methods to make the node move when a swipe is called.
-(void)SwipedDown{
SKAction *Move = [SKAction moveByX:0.00 y:-50 duration:2];
[Main_Character runAction:Move];
}
-(void)SwipedUp{
SKAction *Move = [SKAction moveByX:0.00 y:+50 duration:2];
[Main_Character runAction:Move];
}
-(void)SwipedRight{
SKAction *Move = [SKAction moveByX:+50 y:0.00 duration:2];
[Main_Character runAction:Move];
}
-(void)SwipedLeft{
SKAction *Move = [SKAction moveByX:-50 y:0.00 duration:2];
[Main_Character runAction:Move];
}
Your copy/paste have failed you, you need to fix your setDirections, you only set left, when you want to set other directions.

UISwipeGestureRecognizers won't be recognized simultaneously

In my spritekit game, I am attempting to make it so that 2 UISwipeGestureRecognizers can be recognized simultaneously, however, I am unable to do so. Any help would be much appreciated, thanks in advance. Below is my code...
In ViewDidLoad:
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];
After ViewDidLoad:
-(void)rightSwiped1:(UIGestureRecognizer *)gestureRecognizer {
CGPoint pt = [gestureRecognizer locationInView:self.view];
if(pt.x < (self.view.bounds.size.width/2))
{
SKNode *person1 = [self childNodeWithName:#"person1"];
SKAction *moveRight = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) - 90, CGRectGetMidY(self.frame) + 200) duration:0.1f];
[person1 runAction:moveRight];
} else if (pt.x > (self.view.bounds.size.width/2)) {
SKNode *person2 = [self childNodeWithName:#"person2"];
SKAction *moveRight2 = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) + 400, CGRectGetMidY(self.frame) + 200) duration:0.1f];
[person2 runAction:moveRight2];
}
}
-(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.1f];
[person1 runAction:moveLeft];
} else if (pt.x > (self.view.bounds.size.width/2)) {
SKNode *person2 = [self childNodeWithName:#"person2"];
SKAction *moveLeft2 = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) + 90, CGRectGetMidY(self.frame) + 200) duration:0.1f];
[person2 runAction:moveLeft2];
}
}
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
return YES;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return YES;
}
You never assign the delegate property of your UISwipeGestureRecognizers, therefore your delegate methods never get called.
You can fix this by simply doing:
UISwipeGestureRecognizer *leftSwipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(leftSwiped1:)];
leftSwipe1.delegate = self;
[leftSwipe1 setDirection:UISwipeGestureRecognizerDirectionLeft];
[leftSwipe1 setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:leftSwipe1];
UISwipeGestureRecognizer *rightSwipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(rightSwiped1:)];
rightSwipe1.delegate = self;
[rightSwipe1 setDirection:UISwipeGestureRecognizerDirectionRight];
[rightSwipe1 setNumberOfTouchesRequired:1];
[self.view addGestureRecognizer:rightSwipe1];
Make sure you're also conforming to the UIGestureRecognizerDelegate protocol:
#interface YourViewController <UIGestureRecognizerDelegate>
...
#end

UIPanGestureRecognizer always move from location (0, 0)

I'd like to drag customView.
But, when I drag the customView, it always move from location (0, 0).
Please look at following image.
the red parts is the customView, and when I drag it, it move to the location.
How do I fix it to make move customView correctly?
I have this code.
labelview is the customView(subclass of UIView).
- (void)viewDidLoad
{
[super viewDidLoad];
_labelView = [[LabelView alloc]initWithFrame:CGRectMake(20, 60, 200, 100)];
UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(panAction:)];
[_labelView addGestureRecognizer:pan];
[self.view addSubview:_labelView];
}
- (void)panAction:(UIPanGestureRecognizer *)sender
{
CGPoint p = [sender translationInView:self.view];
CGPoint movedPoint = CGPointMake(_labelView.center.x + p.x, _labelView.center.y + p.y);
sender.view.center = movedPoint;
[sender setTranslation:movedPoint inView:self.view];
}
While the other two answers are technically correct, they are not ideal solutions because when you add a gesture recognizer to self.view then anytime you drag anywhere in self.view you will drag your LabelView, but I imagine you only want to drag it when you actually have your finger on the LabelView itself.
So, just keep what you have, but change this one line:
[sender setTranslation:movedPoint inView:self.view];
to
[sender setTranslation:CGPointZero inView:self.view];
You have implemented the logic upside down. It has to be exactly other way around. Set the ´UIPanGestureRecognizer´ to UIViewController's view and set the center of your custom view.
- (void)viewDidLoad
{
[super viewDidLoad];
_labelView = [[LabelView alloc]initWithFrame:CGRectMake(20, 60, 200, 100)];
UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(panAction:)];
// [_labelView addGestureRecognizer:pan]; must be removed
[self.view addGestureRecognizer:pan]; // must be added
[self.view addSubview:_labelView];
}
- (void)panAction:(UIPanGestureRecognizer *)sender
{
CGPoint p = [sender translationInView:self.view];
CGPoint movedPoint = CGPointMake(_labelView.center.x + p.x, _labelView.center.y + p.y);
[_labelView setCenter:movedPoint];
}
I solved by writing following code.
Thank you for answers.
- (void)viewDidLoad
{
[super viewDidLoad];
_labelView = [[LabelView alloc]initWithFrame:CGRectMake(20, 60, 200, 100)];
UIPanGestureRecognizer* pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(panAction:)];
[_labelView addGestureRecognizer:pan];
[self.view addSubview:_labelView];
}
- (void)panAction:(UIPanGestureRecognizer *)sender
{
CGPoint p = [sender translationInView:sender.view];
CGPoint movedPoint = CGPointMake(sender.view.center.x + p.x, sender.view.center.y + p.y);
sender.view.center = movedPoint;
[sender setTranslation:CGPointZero inView:sender.view];
}

Drag and drop an image from UIPopoverController to UIViewController

I'm showing a UIPopoverController in a ViewController, and I want to drag one image from the popover to the ViewController.
I know that to do it, I need to use a UIGestureRecognizer, but I don't know how. I can move the image around the popover, but I can't drag and drop it to the ViewController.
My code:
-(void)viewDidLoad
{
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(moveImage:)];
[panGesture setMinimumNumberOfTouches:1];
[panGesture setMaximumNumberOfTouches:1];
[self.view addGestureRecognizer:panGesture];
}
- (void)moveImage:(UIPanGestureRecognizer *)recognizer
{
CGPoint newCenter = [recognizer translationInView:self.view];
if([recognizer state] == UIGestureRecognizerStateBegan) {
beginX = ball.center.x;
beginY = ball.center.y;
}
newCenter = CGPointMake(beginX + newCenter.x, beginY + newCenter.y);
[ball setCenter:newCenter];
}

Resources