使用 panGesture 的交互式动画 iOS
interactive animation with panGesture iOS
我有一个 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
@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
}
}
希望对你有所帮助
因为你一直在应用动画。即使您正在移动视图,这也不是必需的。当您移动视图时,该动画会导致中断。
@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)
}
我有一个 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
@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
}
}
希望对你有所帮助
因为你一直在应用动画。即使您正在移动视图,这也不是必需的。当您移动视图时,该动画会导致中断。
@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)
}