简体   繁体   中英

UILabel moving left or right Swift

I have set up 2 text by autolayouts and constraints. Now i would like to move text 1 to the right and text 2 to the left but it didnt work. 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:

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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