大家好我试图让我的动画转到任何一方取决于用户正在平移的方式,但它不起作用,我无法弄清楚什么是错的。当我只使用一个方向时,手势正常工作,但当我将逻辑移动到moveLabel()函数时,它不再起作用。动画甚至没有开始。我的目标是拥有相同的动画(因为我将为其添加更多细节)但是这个特定的标签会根据摇摄方向向左或向右移动。这是我现在的感冒
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var labelDummy: UILabel!
var labelAnimation = UIViewPropertyAnimator()
override func viewDidLoad() {
super.viewDidLoad()
labelDummy.center.x = view.bounds.maxX/2
view.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.moveLabel)))
}
func moveLabel(gesture: UIPanGestureRecognizer){
let trans = gesture.translation(in: view)
if trans.x >= 0{
labelAnimation = UIViewPropertyAnimator(duration: 1.0, curve: .easeInOut) {
let yPos = self.labelDummy.center.y
self.labelDummy.center = CGPoint(x: 100 + (self.labelDummy.frame.size.width/2), y: yPos)
}
}
if trans.x < 0 {
labelAnimation = UIViewPropertyAnimator(duration: 1.0, curve: .easeInOut) {
let yPos = self.labelDummy.center.y
self.labelDummy.center = CGPoint(x: 10 + (self.labelDummy.frame.size.width/2), y: yPos)
}
}
labelAnimation.fractionComplete = abs((trans.x/100))
if gesture.state == .ended{
labelAnimation.fractionComplete = 0
}
print("fractionCompleted: ", labelAnimation.fractionComplete)
}
}
我该如何解决这个问题?
正在运行的代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var labelDummy: UILabel!
var labelAnimation = UIViewPropertyAnimator()
override func viewDidLoad() {
super.viewDidLoad()
labelDummy.center.x = view.bounds.maxX/2
// Pan for hele viewen som spiller label animasjon
view.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.moveLabel)))
// animasjon
labelAnimation = UIViewPropertyAnimator(duration: 1.0, curve: .easeInOut) {
let yPos = self.labelDummy.center.y
self.labelDummy.center = CGPoint(x: 10 + (self.labelDummy.frame.size.width/2), y: yPos)
}
//labelAnimation.startAnimation()
}
func moveLabel(gesture: UIPanGestureRecognizer){
print("retning: ", gesture.velocity(in: view).x)
let trans = gesture.translation(in: view)
print("trans: ", trans)
labelAnimation.fractionComplete = trans.x/100
print("fractionCompletet prøver å settes til : ", trans.x/100)
print(trans.x)
if gesture.state == .ended{
print("-ENDED-")
labelAnimation.fractionComplete = 0
}
print("fractionCompletet: ", labelAnimation.fractionComplete)
}
}
答案 0 :(得分:1)
结束为x位置创建一个全局变量,并在moveLabel函数中使用if语句类似“if .began”,然后检查翻译。根据翻译是高于还是低于0,将xposition设置在左侧的某个位置,或者在右侧的某个位置。
if gesture.state == .began {
if gesture.velocity(in: view).x > 0{
// Panning right, Animate Left
self.animationDirection = .left
self.animateToXPos = CGPoint(x: headerLabelPositionLeft!, y: headerLabelPositionY!)
self.setAnimation(direction: AnimationDirection.left)
self.dayLabel.textAlignment = .left
} else {
// Panning left, Animate Right
self.animationDirection = AnimationDirection.right
self.animateToXPos = CGPoint(x: self.view.bounds.width - (self.dayLabel.frame.size.width/2), y: headerLabelPositionY)
self.setAnimation(direction: AnimationDirection.right)
self.dayLabel.textAlignment = .right
}
}