0

I've got this function that cycles through arrays and changes 2 labels. How can I make it so the UILabels come from the right and leave to the left?

func updateLabels() {
        self.myTimer = Timer.scheduledTimer(withTimeInterval: 2, repeats: true) { (t) in
            self.titleLabel.text = self.titleArray[self.counter]
            self.textLabel.text = self.textArray[self.counter]
            self.counter += 1
            if self.counter == self.titleArray.count && self.counter == self.textArray.count {
                self.counter = 0
            }
        }
    }
vukurigusi
  • 17
  • 6

1 Answers1

0

Move UILabel from Right to left. Add proper constraints to your text label and if you want to get animation from right to left set text Alignment to right

func updateLabels() {

            self.myTimer = Timer.scheduledTimer(withTimeInterval: 2, repeats: true) { (t) in
                self.titleLabel.text = self.titleArray[self.counter]
                self.textLabel.text = self.textArray[self.counter]
               DispatchQueue.main.async(execute: {
                UIView.animate(withDuration: 5.0, delay: 1, options: ([.curveLinear, .repeat]), animations: {() -> Void in
                    self.textLabel.center = CGPoint(x:0 - self.textLabel.bounds.size.width/2, y:self.textLabel.center.y)
                }, completion:  nil)
            })
                self.counter += 1
                if self.counter == self.titleArray.count && self.counter == 
                 self.textArray.count {
                    self.counter = 0
                }
            }
        }
channu
  • 223
  • 3
  • 10
  • Hi! Thank you for your response. The UILabel does go to the right, but I need that at the same time, comes from the left the next tip. Right now, nothing comes from the left, the UILabel just goes away to the right. – vukurigusi Jun 18 '19 at 12:58
  • Also, once the delay ends, it just keeps non-stop repeating the animation. – vukurigusi Jun 18 '19 at 13:07
  • you mean UILabel should come from both direction at same time – channu Jun 19 '19 at 05:31
  • Yes, like |<---UILabel<---|<---UILabel2, the | are the edges of the screen, so it would be like this: UILabel shows up, waits x number of seconds, then changes to UILabel 2 with the slide to the left animation. Like if you had all the UILabels on a wheel, and you just rotate the wheel. So when one is going away, the other one is showing up. It's a fairly common used animation, it's just I don't know how to explain it very well, sorry! – vukurigusi Jun 19 '19 at 12:45