GestureRecogniser没有回应

时间:2017-01-19 14:39:52

标签: ios swift uigesturerecognizer

初学者 - 以下代码在TouchesBegan函数中正常工作。但是不能使用GestureRecongnizer(滑动或点击)。似乎手势识别器根本没有响应。

我正在使用swift 2和xcode 7.3

override func viewDidLoad()
{ ...
let SwipeUp = UISwipeGestureRecognizer(target: self, action:#selector(ViewController.Dragged(_:)))
   SwipeUp.direction = UISwipeGestureRecognizerDirection.Up
    self.egg.addGestureRecognizer(SwipeUp)
 }
  func Dragged(gesture: UIGestureRecognizer)
{
     UIView.animateWithDuration(0.4, delay: 1.0, options: .CurveEaseOut, animations: {
        let eggFrame = self.egg.frame
        eggFrame.origin.y -= 700
        self.egg.frame = eggFrame
        }, completion: { finished in print("done!") } 
}

2 个答案:

答案 0 :(得分:1)

您需要对UIGestureRecognizer保持强烈的引用。

var swipeUp: UISwipeGestureRecognizer!

override func viewDidLoad() {
    swipeUp = UISwipeGestureRecognizer(target: self, action:#selector(ViewController.Dragged(_:)))
    swipeUp.direction = UISwipeGestureRecognizerDirection.Up
    self.egg.addGestureRecognizer(swipeUp)
}

func Dragged(gesture: UIGestureRecognizer) {
    UIView.animateWithDuration(0.4, delay: 1.0, options: .CurveEaseOut, animations: {
        let eggFrame = self.egg.frame
        eggFrame.origin.y -= 700
        self.egg.frame = eggFrame
    }, completion: { finished in print("done!") } 
}

答案 1 :(得分:0)

UIPanGestureRecognizer仅识别您附加到的元素(鸡蛋)上的滑动手势。如果您在鸡蛋视图中快速向上滑动,它应该识别滑动。

如果要添加拖动或点按手势识别器,则必须向鸡蛋添加UITapGestureRecognizer或{{1}}。

您确定没有调用Dragged方法吗?