简体   繁体   English

UILabel向左或向右移动Swift

[英]UILabel moving left or right Swift

I have set up 2 text by autolayouts and constraints. 我已经通过自动布局和约束设置了2个文本。 Now i would like to move text 1 to the right and text 2 to the left but it didnt work. 现在我想将文本1移到右边,将文本2移到左边,但是没有用。 Both the text should end on the center of the view (horizontally) 两个文本均应在视图中心(水平)结束

@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.
}

Did i did anything wrong in the code? 我在代码中做错了什么吗?

You can use CGAffineTransform(translationX: y:) to achieve something like that: 您可以使用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)

To move it back to it's originial place add this in another animation block: 要将其移回其原始位置,请将其添加到另一个动画块中:

self.breathingStatusLabel.transform = .identity

Note: CGAffineTransform(translationX: y:) translates the view to a provided location but it doesn't change the frame of that. 注意: CGAffineTransform(translationX: y:)将视图转换到提供的位置,但不会更改其框架。 So be careful about that while using this, to just animate labels you can use it. 因此,在使用此控件时要特别注意,只需为标签设置动画即可使用它。

But for example you want to move UITextFields when keyboard appears, you should change the constraints if its autolayout or change its frame. 但是,例如,当键盘出现时,您想移动UITextFields ,则应在自动autolayout更改约束或更改其框架。 If you use CGAffineTransform(translationX: y:) you will notice that it will move the UITextField but when you tap on it nothing will work coz its frame didn't change, just the location changed. 如果使用CGAffineTransform(translationX: y:)您会注意到它会移动UITextField但是当您点击它时,它将无法工作,因为其框架没有改变,只是位置发生了变化。

You need to play with constraints of labels. 您需要使用标签约束。

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)
@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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM