panGesture iOS 的交互式动画
Posted
技术标签:
【中文标题】panGesture iOS 的交互式动画【英文标题】:interactive animation with panGesture iOS 【发布时间】:2018-05-25 05:06:32 【问题描述】:我有一个 sheetView,其顶部边缘始终以父视图中心为中心。
我允许它使用平移手势垂直拖动,当手势结束时,我将 sheetView 动画化回父视图的中心。
但我想在 sheetView 仍在动画时与其交互,并在平移时垂直更改其位置,但我无法做到,它总是完成当前动画然后开始下一个动画。
override func viewWillAppear(_ animated: Bool)
super.viewWillAppear(animated)
sheetView.frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height)
middlePoint = CGPoint(x: view.bounds.midX, y: view.bounds.height)
sheetView.center = middlePoint
@IBAction func handlePan(_ sender: UIPanGestureRecognizer)
let recognizer = sender
let translation = recognizer.translation(in: self.view)
if recognizer.state == .began
if animator.isRunning
animator.stopAnimation(true)
animator.addAnimations
self.sheetView.center = self.middlePoint
animator.pauseAnimation()
else if recognizer.state == .changed
self.sheetView.center.y += translation.y
else if recognizer.state == .ended || recognizer.state == .cancelled
if animator.isRunning
animator.stopAnimation(true)
animator.addAnimations
self.sheetView.center = self.middlePoint
animator.startAnimation()
recognizer.setTranslation(CGPoint.zero, in: self.view)
你可以在这里找到整个项目:sheetView
【问题讨论】:
【参考方案1】:因为你总是在应用动画。即使您正在移动视图,这也不是必需的。当您移动视图时,该动画会导致中断。
@IBAction func handlePan(_ sender: UIPanGestureRecognizer)
let recognizer = sender
let translation = recognizer.translation(in: self.view)
if recognizer.state == .began
self.sheetView.center = self.middlePoint
else if recognizer.state == .changed
self.sheetView.center.y += translation.y
else if recognizer.state == .ended || recognizer.state == .cancelled
if animator.isRunning
animator.stopAnimation(true)
animator.addAnimations
self.sheetView.center = self.middlePoint
animator.startAnimation()
recognizer.setTranslation(CGPoint.zero, in: self.view)
【讨论】:
如果您有任何问题,请告诉我。 谢谢,它确实帮助我很好地理解了。 已投赞成票,但由于我的声望低于 15,因此未公开显示【参考方案2】:@objc func panGestureAction(_gestureRecognizer : UIPanGestureRecognizer)
guard gestureRecognizer.view != nil else return
let directionVelocity = gestureRecognizer.velocity(in: myView)
switch gestureRecognizer.state
case .changed:
if directionVelocity.x > 0
//print("swipe right")
if directionVelocity.x < 0
//print("swipe left")
if directionVelocity.y > 0
//print("swipe down")
if directionVelocity.y < 0
//print("swipe up")
break
case .ended :
//print(“Ended”)
default:
break
希望对你有帮助。
【讨论】:
嗯,这不是我所期望的,但是你帮我找到了进一步工作的方向。谢谢以上是关于panGesture iOS 的交互式动画的主要内容,如果未能解决你的问题,请参考以下文章