iphone - 如何初始化UIGestureRecognizer?

标签 iphone ios uigesturerecognizer

我想在我的按钮上添加一个手势识别器,以便在用户滑过按钮框架时可以运行代码。如果向上,向右,向左或向下滑动按钮,我也希望此代码有所不同。

-(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

        }

最佳答案

您可能想使用UISwipeGestureRecognizer。通常不应该使用UIGestureRecognizer,除非您将其子类化。您的代码应类似于以下内容。

UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(detectSwipe)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
[button addGestureRecognizer:swipe];

关于iphone - 如何初始化UIGestureRecognizer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11925729/

相关文章:

android - 检测 Spotify 歌曲播放

iphone - 如何在ios中将FLAC文件转换为wav文件?

iphone - 如何退出 NSThread

iOS - 在关闭和滚动手势之间切换

ios - UITapGestureRecognizer - 单击和双击

iphone - 如何获取沙盒中文档的所有文件名?

ios - 呈现 UIAlertController 时出现 CGContext 错误

iOS:在启用自动布局的情况下在代码中设置 View 大小

ios - 检查 firebase 数据库中是否存在用户名。

ios - 我不明白为什么这个 @selector 不起作用