Reputation: 108
Beginner here- the below code works fine in TouchesBegan function. But doesn't work with GestureRecongnizer (swipe or tap). It seems like the gesture recognizer doesn't respond at all.
I'm working on swift 2 and 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!") }
}
Upvotes: 0
Views: 97
Reputation: 1378
You need to keep a strong reference to the 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!") }
}
Upvotes: 1
Reputation: 111
The UISwipeGestureRecognizer
only recognizes swipe gestures on the element you attached it to (the egg). If you do a rapid swipe up in the egg view it should recognize the swipe.
If you want to add Drag or Tap gesture recognizers, you have to add UIPanGestureRecognizer
or UITapGestureRecognizer
to the egg.
Are you sure that the Dragged method is not being called?
Upvotes: 0