检测平移手势结束

发布于 2024-11-17 03:52:13 字数 539 浏览 3 评论 0原文

我有一个视图,并将 UIPanGestureRecogniser 应用于该视图:

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAnim:)];
[sliderView addGestureRecognizer:panGesture];
[panGesture release];

我可以很好地检测和处理手势。但是,我希望在手势结束后启动另一种方法。

我知道有两种方法可以进行这种检测。 touchesEndedtouchesCancelled 但是,我发现一旦触摸变成手势,即我移动手指足以保证,touchesCancelled 就会被调用手势调用和 touchesEnded 很少(如果有的话)被调用。

我希望能够向左/向右平移,然后在手势结束时启动另一个函数调用。我该怎么做?

I've got a view and I applied a UIPanGestureRecogniser to this view:

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAnim:)];
[sliderView addGestureRecognizer:panGesture];
[panGesture release];

I can detect and process the gesture just fine. However, I wish to initiate another method once the gesture has ended.

I know there are two methods that allow this kind of detection. touchesEnded and touchesCancelled however, I've found that touchesCancelled gets called as soon as the touch becomes a gesture i.e. I move my finger enough to warrant a gesture call and touchesEnded rarely, if ever, gets called.

I want to be able to pan left / right and then initiate another function call upon gesture ending. How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

白色秋天 2024-11-24 03:52:13

可以通过使用 UIGestureRecognizerStateEnded 检查其状态来检测平移手势结束事件。

检查下面的代码。

-(void) panAnim:(UIPanGestureRecognizer*) gestureRecognizer
{
   if(gestureRecognizer.state == UIGestureRecognizerStateEnded)
   {
      //All fingers are lifted.
   }
}

来自苹果文档

平移手势是连续的。它
开始(UIGestureRecognizerStateBegan)
当手指数量最少时
允许 (minimumNumberOfTouches) 有
移动得足以被认为是平底锅。
它改变了
(UIGestureRecognizerStateChanged) 当
手指移动时至少
按下的手指最少数量
向下。结束了
(UIGestureRecognizerStateEnded) 当
所有手指都抬起。

了解有关 UIPanGestureRecognizer 的更多信息

Pan gesture end event can be detected by checking its state with UIGestureRecognizerStateEnded.

Check with the below code .

-(void) panAnim:(UIPanGestureRecognizer*) gestureRecognizer
{
   if(gestureRecognizer.state == UIGestureRecognizerStateEnded)
   {
      //All fingers are lifted.
   }
}

From Apple documentation

A panning gesture is continuous. It
begins (UIGestureRecognizerStateBegan)
when the minimum number of fingers
allowed (minimumNumberOfTouches) has
moved enough to be considered a pan.
It changes
(UIGestureRecognizerStateChanged) when
a finger moves while at least the
minimum number of fingers are pressed
down. It ends
(UIGestureRecognizerStateEnded) when
all fingers are lifted.

Read more about UIPanGestureRecognizer

二手情话 2024-11-24 03:52:13

可以通过使用 UIGestureRecognizerStateEndedUIGestureRecognizerStateCancelledUIGestureRecognizerStateFailed 检查其状态来检测平移手势结束事件,

请使用以下代码进行检查。

   -(void) panGesture:(UIPanGestureRecognizer*) gestureRecognizer
    {
     if(gestureRecognizer.state == UIGestureRecognizerStateEnded || gestureRecognizer.state == UIGestureRecognizerStateFailed || gestureRecognizer.state == UIGestureRecognizerStateCancelled)
             {
                //code what you want.
             }
     }

Pan gesture end event can be detected by checking the it's state with UIGestureRecognizerStateEnded or UIGestureRecognizerStateCancelled or UIGestureRecognizerStateFailed

Check with the below code .

   -(void) panGesture:(UIPanGestureRecognizer*) gestureRecognizer
    {
     if(gestureRecognizer.state == UIGestureRecognizerStateEnded || gestureRecognizer.state == UIGestureRecognizerStateFailed || gestureRecognizer.state == UIGestureRecognizerStateCancelled)
             {
                //code what you want.
             }
     }
离不开的别离 2024-11-24 03:52:13

上面的答案都是正确的,这只是 Swift 的更新答案。

斯威夫特3:

func panGesture(recognizer: UIPanGestureRecognizer) {
    if recognizer.state == .ended {
        // Do what you want
    }
}

Above answers are all correct, this is just an updated one for Swift.

Swift 3:

func panGesture(recognizer: UIPanGestureRecognizer) {
    if recognizer.state == .ended {
        // Do what you want
    }
}
过潦 2024-11-24 03:52:13

在 Swift 4 中,使用 UIGestureRecognizerState.ending。

例如,

if (gestureRecognizer.state == UIGestureRecognizerState.ended) {

        //Move label back to original position (function invoked when gesture stops)
        UIView.animate(withDuration: 0.4) {
            self.swipeLabel.center = CGPoint(x: self.view.bounds.width / 2, y: self.view.bounds.height / 2)
        }
    }

下面是视图控制器中使用手势对 UILabel 进行动画处理所需的所有代码,包括手势结束时的代码。

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var swipeLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    //Create gesture
    let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(moveLabelBasedOn(gestureRecognizer:)))

    //Assign gesture to UILabel
    swipeLabel.addGestureRecognizer(gestureRecognizer)

}

//Animate Label in Resopnse to Gesture
@objc func moveLabelBasedOn(gestureRecognizer: UIPanGestureRecognizer) {

    let changeInPosition = gestureRecognizer.translation(in: view)

    //Move label in response to gesture
    swipeLabel.center = CGPoint(x: view.bounds.width / 2 + changeInPosition.x, y: view.bounds.height / 2 + changeInPosition.y)

    //Check if gesture ended
    if (gestureRecognizer.state == UIGestureRecognizerState.ended) {

        //Move label back to original position (function invoked when gesture stops)
        UIView.animate(withDuration: 0.4) {
            self.swipeLabel.center = CGPoint(x: self.view.bounds.width / 2, y: self.view.bounds.height / 2)
        }
    }
}

}

希望这有帮助。

In Swift 4, use UIGestureRecognizerState.ended.

e.g.

if (gestureRecognizer.state == UIGestureRecognizerState.ended) {

        //Move label back to original position (function invoked when gesture stops)
        UIView.animate(withDuration: 0.4) {
            self.swipeLabel.center = CGPoint(x: self.view.bounds.width / 2, y: self.view.bounds.height / 2)
        }
    }

Below is all the code you need in a view controller to animate a UILabel with gesture, including when the gesture ends.

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var swipeLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    //Create gesture
    let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(moveLabelBasedOn(gestureRecognizer:)))

    //Assign gesture to UILabel
    swipeLabel.addGestureRecognizer(gestureRecognizer)

}

//Animate Label in Resopnse to Gesture
@objc func moveLabelBasedOn(gestureRecognizer: UIPanGestureRecognizer) {

    let changeInPosition = gestureRecognizer.translation(in: view)

    //Move label in response to gesture
    swipeLabel.center = CGPoint(x: view.bounds.width / 2 + changeInPosition.x, y: view.bounds.height / 2 + changeInPosition.y)

    //Check if gesture ended
    if (gestureRecognizer.state == UIGestureRecognizerState.ended) {

        //Move label back to original position (function invoked when gesture stops)
        UIView.animate(withDuration: 0.4) {
            self.swipeLabel.center = CGPoint(x: self.view.bounds.width / 2, y: self.view.bounds.height / 2)
        }
    }
}

}

Hope this helps.

万人眼中万个我 2024-11-24 03:52:13

这对于两个(或更多)手指平底锅不起作用。在这种情况下,使用 numberOfMinimumTouches = 2,平移将开始,您可以滚动/平移,但如果您抬起一根手指,平移仍将继续(移动到一根手指所在的位置,而不是在之间移动)两个手指),因为仅当所有手指抬起时才会发生State.ending。要根据 numberOfMinimumTouches 值停止,需要实现不同的方法。

This would not work for a two (or more) finger pan. In this case with numberOfMinimumTouches = 2, the pan would start and you can scroll/pan around, but if you lift one finger, the pan will still continue (move to where the one finger is instead of between the two fingers), since the State.ended is only when ALL fingers are lifted. To stop based on the numberOfMinimumTouches value, a different approach needs to be implemented.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文