如何忽略滑动手势要使用touchBegan

时间:2015-12-11 09:29:16

标签: ios objective-c iphone xcode swipe

当我每次触摸屏幕时运行此程序时,我希望球向下移动。问题在于每次我这样做时,滑动手势(向右移动)也会在我不想要的时候识别触摸。这将导致球侧身移动(应用触摸和滑动)。

有没有办法忽略滑动手势,所以我的touchBegan是独自一人。

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

2 个答案:

答案 0 :(得分:1)

这可能会有所帮助

swipeRight.cancelsTouchesInView = YES

答案 1 :(得分:0)

我认为如果你只想向下移动你的对象,那么不要在你的Ball.center.x方法中使用touchesBegan,而是使用x的常量值

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

并使用

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