Here is IceFish's answer in swift:
override func viewDidLoad() {
super.viewDidLoad()
let swipeRight = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
self.view.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: "respondToSwipeGesture:")
swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
self.view.addGestureRecognizer(swipeLeft)
and the responder:
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Left:
UIView.animateWithDuration(0.1, animations: {
let myFrame = self.view.frame
self.view.frame = CGRectMake(-myFrame.size.width, myFrame.origin.y , myFrame.size.width, myFrame.size.height)
}, completion: {_ in
let myFrame = self.view.frame
self.view.frame = CGRectMake(myFrame.size.width, myFrame.origin.y , myFrame.size.width, myFrame.size.height)
UIView.animateWithDuration(0.3, animations: {
let myFrame = self.view.frame
self.view.frame = CGRectMake(0.0, myFrame.origin.y , myFrame.size.width, myFrame.size.height)
}, completion: nil)
})
// do left action here
case UISwipeGestureRecognizerDirection.Right:
UIView.animateWithDuration(0.1, animations: {
let myFrame = self.view.frame
self.view.frame = CGRectMake(myFrame.size.width, myFrame.origin.y , myFrame.size.width, myFrame.size.height)
}, completion: {_ in
let myFrame = self.view.frame
self.view.frame = CGRectMake(-myFrame.size.width, myFrame.origin.y , myFrame.size.width, myFrame.size.height)
UIView.animateWithDuration(0.3, animations: {
let myFrame = self.view.frame
self.view.frame = CGRectMake(0.0, myFrame.origin.y , myFrame.size.width, myFrame.size.height)
}, completion: nil)
})
// do right action here
default:
break
}
}
}