如何通过向下拖动(平移)关闭 UITableViewController
How to close UITableViewController by dragging down(panning)
我正在制作 iOS 应用程序。
现在我不得不做以下事情。
- 详情画面作为模态画面弹出
- 用户想关闭模态 window 就像 Twitter 的照片屏幕一样向下拖动
并且我尝试参考 How to use UIPanGestureRecognizer to move object? iPhone/iPad
进行编码
我的代码如下所示。
它使 tableView 移动到奇怪的方向然后关闭模态。
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)
}
}
有没有人指出我正确的方向?
提前致谢。
您将需要创建自定义模态动画。你应该阅读 Customizing the Transition Animations from the Presentation and Transitions section of the View Controller Programming Guide for iOS.
具体来说 Adding Interactivity to Your Transitions 中有一部分内容,但您可能需要阅读更多内容才能理解。
简而言之,您将必须创建 UIViewControllerTransitioningDelegate
and assign that to the transitioning delegate of the view controller being presented. The job of the UIViewControllerTransitioningDelegate
的一个实现是将许多其他对象出售给 UIKit
,这些对象处理自定义动画(以及可选的演示)以呈现和关闭视图控制器。
除了你的执行UIViewControllerTransitioningDelegate
you will also have to create implementations of UIViewControllerAnimatedTransitioning
and UIViewControllerInteractiveTransitioning
. These objects will ultimately be the ones performing the relevant animations, and they are the objects vended to UIKit
by your UIViewControllerTransitioningDelegate
执行。
我正在制作 iOS 应用程序。
现在我不得不做以下事情。
- 详情画面作为模态画面弹出
- 用户想关闭模态 window 就像 Twitter 的照片屏幕一样向下拖动
并且我尝试参考 How to use UIPanGestureRecognizer to move object? iPhone/iPad
进行编码我的代码如下所示。 它使 tableView 移动到奇怪的方向然后关闭模态。
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)
}
}
有没有人指出我正确的方向? 提前致谢。
您将需要创建自定义模态动画。你应该阅读 Customizing the Transition Animations from the Presentation and Transitions section of the View Controller Programming Guide for iOS.
具体来说 Adding Interactivity to Your Transitions 中有一部分内容,但您可能需要阅读更多内容才能理解。
简而言之,您将必须创建 UIViewControllerTransitioningDelegate
and assign that to the transitioning delegate of the view controller being presented. The job of the UIViewControllerTransitioningDelegate
的一个实现是将许多其他对象出售给 UIKit
,这些对象处理自定义动画(以及可选的演示)以呈现和关闭视图控制器。
除了你的执行UIViewControllerTransitioningDelegate
you will also have to create implementations of UIViewControllerAnimatedTransitioning
and UIViewControllerInteractiveTransitioning
. These objects will ultimately be the ones performing the relevant animations, and they are the objects vended to UIKit
by your UIViewControllerTransitioningDelegate
执行。