UIViewPropertyAnimator 根据平移方向动画移动
UIViewPropertyAnimator animating movement depending on pan direction
大家好,我正在尝试让我的动画根据用户平移的方式向任意一侧移动,但它不起作用,我也不知道出了什么问题。当我只使用一个方向时,手势工作正常,但当我将逻辑移至 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)
}
}
最终为 x 位置创建了一个全局变量,在 moveLabel 函数中我使用了类似 "if .began " 的 if 语句,然后检查翻译。根据翻译是高于还是低于 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
}
}
大家好,我正在尝试让我的动画根据用户平移的方式向任意一侧移动,但它不起作用,我也不知道出了什么问题。当我只使用一个方向时,手势工作正常,但当我将逻辑移至 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)
}
}
最终为 x 位置创建了一个全局变量,在 moveLabel 函数中我使用了类似 "if .began " 的 if 语句,然后检查翻译。根据翻译是高于还是低于 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
}
}