我已经实现了带有手势的动画,并且由于动画看起来不流畅并且视图位置未设置为其实际起始位置而面临一些问题。
请看以下图片以了解动画。
以下是我的代码来处理平移手势并根据动画移动视图。
@objc func wasDragged(gestureRecognizer: UIPanGestureRecognizer) {
//return
print(gestureRecognizer.state.rawValue)
let direction = gestureRecognizer.direction(in: gestureRecognizer.view!)
if gestureRecognizer.state == UIGestureRecognizerState.began || gestureRecognizer.state == UIGestureRecognizerState.changed {
let translation = gestureRecognizer.translation(in: self.view)
print(gestureRecognizer.view!.center.y)
if((gestureRecognizer.view!.center.y) < minY){
gestureRecognizer.view!.center = CGPoint(x:gestureRecognizer.view!.center.x, y:(minY - 1))
}
if(gestureRecognizer.view!.center.y < (self.pagingView.frame.size.height - 100) && gestureRecognizer.view!.center.y > (minY - 1)) {
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x, y: gestureRecognizer.view!.center.y + translation.y)
}else {
if(direction == .Up){
print("UP")
if(gestureRecognizer.view!.center.y > (minY - 1 )){
gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x, y: gestureRecognizer.view!.center.y + translation.y)
}else{
gestureRecognizer.view!.center = CGPoint(x:gestureRecognizer.view!.center.x, y:CGFloat(minY))
}
}else if(direction == .Down){
print("Down")
if((gestureRecognizer.view!.center.y - (minY-1)) > 100){
gestureRecognizer.view!.center = CGPoint(x:gestureRecognizer.view!.center.x, y:(self.pagingView.frame.size.height - 100))
}else{
gestureRecognizer.view!.center = CGPoint(x:gestureRecognizer.view!.center.x, y:minY)
}
}
}
gestureRecognizer.setTranslation(CGPoint(x: 0, y: 0), in: self.view)
}
}
上面的代码面临的问题是视图转换不平滑。如果用户从屏幕的一半移开手指,应该自动将视图设置为初始部分,该如何实现逻辑呢?