bme*_*nde 1 iphone uigesturerecognizer ios
我想在我的按钮上添加一个手势识别器,以便我可以在用户滑过按钮框架时运行代码.如果滑动向上,向右,向左或向下按钮,我也希望此代码不同.
-(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];
}
Run Code Online (Sandbox Code Playgroud)
所以,我做的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
}
Run Code Online (Sandbox Code Playgroud)
小智 5
不,这不对.手势识别器的目标不是按钮,它是在检测到手势时调用动作方法的对象(否则它将如何知道哪个对象调用该方法?在OO中,方法调用/消息发送需要显式方法名称和实例或类).
所以你很可能想要
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
Run Code Online (Sandbox Code Playgroud)
您也不直接创建UIGestureRecognizer的实例,但如果是具体的子类,则在本例中为UISwipeGestureRecognizer.
在分配识别器之后,将它附加到您想要识别的视图:
[button addGestureRecognizer:recognizer];
Run Code Online (Sandbox Code Playgroud)
然后在didSwipe:方法中,您可以使用手势识别器的属性来确定滑动的大小/距离/其他属性.
| 归档时间: |
|
| 查看次数: |
1601 次 |
| 最近记录: |