I'm working on making an iOS app.
Now I'm stuck to do the following stuff.
- detail screen is popped up as modal screen
- user wants to close the modal window by dragging down like Twitter's photo screen
And I tried to code by reference to How to use UIPanGestureRecognizer to move object? iPhone/iPad
My code looks like below. It makes tableView move to strange direction and then close the modal.
var firstX: CGFloat = 0
var firstY: CGFloat = 0
var finalX: CGFloat = 0
var finalY: CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
let recognizer : UIPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: "move:")
recognizer.minimumNumberOfTouches = 1
recognizer.maximumNumberOfTouches = 1
self.tableView.addGestureRecognizer(recognizer)
}
func move(sender : UIPanGestureRecognizer) {
print("move")
self.view.bringSubviewToFront(tableView)
var translatedPoint : CGPoint = sender.translationInView(self.view)
if sender.state == UIGestureRecognizerState.Began {
firstX = 0 // (sender.view?.center.x)!
firstY = (tableView?.center.y)!
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY)
tableView?.center = translatedPoint
if sender.state == UIGestureRecognizerState.Ended {
let velocityY = 0.2 * sender.velocityInView(self.view).y
finalX = firstX //translatedPoint.x + velocityX
finalY = translatedPoint.y + velocityY
if (UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation)) {
if finalY < 0 {
finalY = 0
} else if finalY > 1024 {
finalY = 1024
}
} else {
if finalY < 0 {
finalY = 0
} else if finalY > 768 {
finalY = 1024
}
}
let animationDuration = ( abs(velocityY) * 0.0002 ) + 0.2
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(Double(animationDuration))
UIView.setAnimationCurve(UIViewAnimationCurve.EaseOut)
UIView.setAnimationDelegate(self)
UIView.setAnimationDidStopSelector("animationDidFinish")
self.view.center = CGPointMake(finalX, finalY)
UIView.commitAnimations()
}
}
func animationDidFinish() {
print("animationDidFinish")
if finalY > 50 {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
Does anyone point me to the right direction? Thanks in advance.