识别用户剪切/滑动手势(UISwipeGestureRecognizer)

时间:2010-12-27 03:29:20

标签: iphone cocoa-touch uicontrol

我使用下面的代码并为'self.view'工作,但不适用于imageview和方向只是可以添加一个方向如何使用四个方向(右,左,uo,下)?

UISwipeGestureRecognizer *swipe =[[UISwipeGestureRecognizer alloc]initWithTarget:selfaction:@selector(sr)];
swipe.direction =UISwipeGestureRecognizerDirectionRight;
[imageview addGestureRecognizer:swipe];
[swipe release];

4 个答案:

答案 0 :(得分:2)

代表

touchesBegan: 

touchesEnded:

答案 1 :(得分:2)

// --------在imageview分配------- //

中添加此代码
        imageview.userInteractionEnabled = YES;

对于四个方向,你必须使用两个UISwipeGestureRecognizer: -

      UISwipeGestureRecognizer *swipeRightLeft =[[UISwipeGestureRecognizer     alloc]initWithTarget:selfaction:@selector(sr)];

   [swipeRightLeft setDirection:(UISwipeGestureRecognizerDirectionRight |UISwipeGestureRecognizerDirectionLeft )];

   [imageview addGestureRecognizer:swipeRightLeft];

    [swipeRightLeft release];

    UISwipeGestureRecognizer *swipeUpDown =[[UISwipeGestureRecognizer     alloc]initWithTarget:selfaction:@selector(sr)];

      [swipeUpDown setDirection:(UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown )];

    [imageview addGestureRecognizer:swipeUpDown];

    [swipeUpDown release];

答案 2 :(得分:1)

尝试启用互动:

yourImageView.userInteractionEnabled = YES;

并添加一些指示,如下所示:

swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;

答案 3 :(得分:0)

如果你的应用只能在iOS 3.2+上运行,那么最好的方法就是使用UIGestureRecognizer API类。实际上,有一个UISwipeGestureRecognizer子类(docs are here)。

要做你想做的事,做这样的事情:

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

要更改为任何其他方向,请查看上面的文档链接 - 所有四个方向都有一个常量。