我想向我的按钮添加一个手势识别器,以便在用户滑过按钮框架时我可以运行代码。如果向上、向右、向左或向下滑动按钮,我还希望此代码有所不同。
-(void)viewDidLoad
{
[super viewDidLoad];
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(0, 0, 100, 100);
[self.view addSubview:button];
UIGestureRecognizer *swipe=[[UIGestureRecognizer alloc]initWithTarget:button action:@selector(detectSwipe)];
[button addGestureRecognizer:swipe];
}
那么,我做对了initWithTarget:action:
吗?既然我这样做了,我该如何实现该detectSwipe
方法?
这是我关于如何实施的想法detectSwipe
-(IBAction)detectSwipe:(UIButton *)sender
{
/* I dont know how to put this in code but i would need something like,
if (the swipe direction is forward and the swipe is > sender.frame ){
[self ForwardSwipeMethod];
} else if //same thing for right
else if //same thing for left
else if //same thing for down
}