UILabel 向左或向右移动 Swift
UILabel moving left or right Swift
我已经通过自动布局和约束设置了 2 个文本。现在我想将文本 1 向右移动,将文本 2 向左移动,但它不起作用。两个文本都应该在视图的中心结束(水平)
@IBOutlet weak var Text1: UILabel!
@IBOutlet weak var Text2: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
UIView.animate(withDuration: 5, delay: 0, options: [.beginFromCurrentState],
animations: {
self.Text1.frame.origin.x += 300
self.Text2.frame.origin.x -= 300
self.view.layoutIfNeeded()
}, completion: nil)
// Do any additional setup after loading the view.
}
我在代码中做错了什么吗?
您需要使用标签的约束。
UIView.animate(withDuration: 5, delay: 0, options: [.beginFromCurrentState],
animations: {
//here modify constraints constant or may need to add some new constraints dynamically.
// self.Text1.frame.origin.x += 300
// self.Text2.frame.origin.x -= 300
// self.view.layoutIfNeeded()
}, completion: nil)
您可以使用 CGAffineTransform(translationX: y:)
实现类似的目的:
UIView.animate(withDuration: 1.0, delay: 0.0, options: .curveEaseInOut, animations: {
self.breathingStatusLabel.transform = CGAffineTransform(translationX: self.breathingStatusLabel.bounds.origin.x + 300, y: self.breathingStatusLabel.bounds.origin.y)
}, completion: nil)
要将其移回原来的位置,请将其添加到另一个动画块中:
self.breathingStatusLabel.transform = .identity
注意: CGAffineTransform(translationX: y:)
将视图转换到提供的位置,但不会更改其框架。所以在使用它时要小心,只要动画标签就可以使用它。
但是例如你想在键盘出现时移动UITextFields
,如果它的autolayout
或者改变它的框架,你应该改变约束。如果你使用 CGAffineTransform(translationX: y:)
你会注意到它会移动 UITextField
但是当你点击它时没有任何效果因为它的框架没有改变,只是位置改变了。
@IBOutlet weak var Text1: UILabel!
@IBOutlet var Text1LeftConstrin:NSLayoutConstraint!
// (Connect this with your Text1 label left constrain)
@IBOutlet weak var Text2: UILabel!
@IBOutlet var Text1LeftConstrin:NSLayoutConstraint!
//(Connect this with your Text1 label left constrain)
override func viewDidLoad() {
super.viewDidLoad()
UIView.animate(withDuration: 5, delay: 0, options: [.beginFromCurrentState],
animations: {
Text1.constant += 300
Text2.constant -= 300
self.view.layoutIfNeeded()
}, completion: nil)
// Do any additional setup after loading the view.
}
UIView.transition(with:self.yourLabel,
duration: 5.0,
options: [.autoreverse,.repeat],
animations: {
self.yourLabel.transform = CGAffineTransform(translationX: (-1 * (self.view.frame.size.width / 2) + 20), y:0)
self.yourLabel.transform = CGAffineTransform(translationX: ((self.view.frame.size.width / 2) - 20), y:0)
},
completion: nil)
我已经通过自动布局和约束设置了 2 个文本。现在我想将文本 1 向右移动,将文本 2 向左移动,但它不起作用。两个文本都应该在视图的中心结束(水平)
@IBOutlet weak var Text1: UILabel!
@IBOutlet weak var Text2: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
UIView.animate(withDuration: 5, delay: 0, options: [.beginFromCurrentState],
animations: {
self.Text1.frame.origin.x += 300
self.Text2.frame.origin.x -= 300
self.view.layoutIfNeeded()
}, completion: nil)
// Do any additional setup after loading the view.
}
我在代码中做错了什么吗?
您需要使用标签的约束。
UIView.animate(withDuration: 5, delay: 0, options: [.beginFromCurrentState],
animations: {
//here modify constraints constant or may need to add some new constraints dynamically.
// self.Text1.frame.origin.x += 300
// self.Text2.frame.origin.x -= 300
// self.view.layoutIfNeeded()
}, completion: nil)
您可以使用 CGAffineTransform(translationX: y:)
实现类似的目的:
UIView.animate(withDuration: 1.0, delay: 0.0, options: .curveEaseInOut, animations: {
self.breathingStatusLabel.transform = CGAffineTransform(translationX: self.breathingStatusLabel.bounds.origin.x + 300, y: self.breathingStatusLabel.bounds.origin.y)
}, completion: nil)
要将其移回原来的位置,请将其添加到另一个动画块中:
self.breathingStatusLabel.transform = .identity
注意: CGAffineTransform(translationX: y:)
将视图转换到提供的位置,但不会更改其框架。所以在使用它时要小心,只要动画标签就可以使用它。
但是例如你想在键盘出现时移动UITextFields
,如果它的autolayout
或者改变它的框架,你应该改变约束。如果你使用 CGAffineTransform(translationX: y:)
你会注意到它会移动 UITextField
但是当你点击它时没有任何效果因为它的框架没有改变,只是位置改变了。
@IBOutlet weak var Text1: UILabel!
@IBOutlet var Text1LeftConstrin:NSLayoutConstraint!
// (Connect this with your Text1 label left constrain)
@IBOutlet weak var Text2: UILabel!
@IBOutlet var Text1LeftConstrin:NSLayoutConstraint!
//(Connect this with your Text1 label left constrain)
override func viewDidLoad() {
super.viewDidLoad()
UIView.animate(withDuration: 5, delay: 0, options: [.beginFromCurrentState],
animations: {
Text1.constant += 300
Text2.constant -= 300
self.view.layoutIfNeeded()
}, completion: nil)
// Do any additional setup after loading the view.
}
UIView.transition(with:self.yourLabel,
duration: 5.0,
options: [.autoreverse,.repeat],
animations: {
self.yourLabel.transform = CGAffineTransform(translationX: (-1 * (self.view.frame.size.width / 2) + 20), y:0)
self.yourLabel.transform = CGAffineTransform(translationX: ((self.view.frame.size.width / 2) - 20), y:0)
},
completion: nil)