VictorLe
VictorLe

Reputation: 9

How to Ignore Swipe gesture To use touchBegan

When i run this program every time I touch the screen I would like the ball to move down. The problem is that every time I do that, the Swipe gesture(Moving Right) recognize the touch as well when I don't want it too. Which will cause the ball to move Sideway(Applied the touch and swipe).

Is there a way to ignore the Swipe gesture so my touchBegan is alone.

ViewController.h

int MoveBallRight;
int MoveBallDown;
@interface ViewController : UIViewController{

    __weak IBOutlet UIImageView *Ball;

    __weak IBOutlet UIButton *StartButton;

}
- (IBAction)StartButton:(id)sender;


@end

ViewController.m

@interface ViewController ()

@end

@implementation ViewController

- (IBAction)StartButton:(id)sender {
    MoveBallRight = 30;
    MoveBallDown = 30;


    StartButton.hidden = YES;


    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight)]; swipeRight.direction = UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:swipeRight];



}
-(void)handleSwipeRight{

    Ball.center = CGPointMake(Ball.center.x + MoveBallRight, Ball.center.y);

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    Ball.center = CGPointMake(Ball.center.x , Ball.center.y + MoveBallDown);

}
- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end

Upvotes: 1

Views: 113

Answers (2)

arpan.r
arpan.r

Reputation: 1981

i think if you only want to move your object downward, then instead of using Ball.center.x in your touchesBegan method use constant value for x

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    Ball.center = CGPointMake(<Use Constant here> , Ball.center.y + MoveBallDown);
}

and use

swipeLeft.cancelsTouchesInView=YES;
swipeRight.cancelsTouchesInView=YES;

Upvotes: 0

vaibby
vaibby

Reputation: 1285

THis might help

swipeRight.cancelsTouchesInView = YES

Upvotes: 1

Related Questions