I want to create a marquee label in Swift. I tried some codes but could not get it to work.
I also can do this with animation but I have problem with repeating it.
check this code
var check = true
var speed = 2
var stopWidth = 200.0
override func viewDidLoad() {
label.center.x = view.center.x // Place it in the center x of the view.
label.center.x -= view.bounds.width // Place it on the left of the view with the width = the
Timer.scheduledTimer(timeInterval: TimeInterval(speed),
target: self,
selector: #selector(selectCityViewController.sliderAnimationTime),
userInfo: nil,
repeats: true)
}
@objc func sliderAnimationTime() {
// do what should happen when timer triggers an event
UIView.animate(withDuration: TimeInterval(speed), delay: 0, options: [.curveEaseOut], animations: {
if self.check {
self.check = false
self.label.center.x -= self.view.bounds.width - CGFloat(self.stopWidth)
self.view.layoutIfNeeded()
}else{
self.check = true
self.label.center.x += self.view.bounds.width - CGFloat(self.stopWidth)
self.view.layoutIfNeeded()
}
}, completion: nil)
}
Using a label which can Marquee up its content is really simple. Just add MarqueeLabel pod in your project.
Swift:
pod 'MarqueeLabel/Swift'
And then select the label you wish to perform Marquee on and add the Custom Class MarqueeLabel to it in the Identity Inspector.
That's it.
This is the Simplest way to add marquee in your Label. After adding Custom Class MarqueeLabel if you want some spacing in between the last character and the first character of the content of your label then:
Step 1: Select the label.
Step 2: Go to Attributes Inspector and then increase the fadeLength attribute value the much you want to have. Applying value 10 to it is fair enough.
If you wish to customize more then Add custom class MarqueeLabel to the Label and then take the outlet of that Label in you Code and customize it the way you want to.
The outlet of that Label in your code should look like this:
@IBOutlet var YOURLABELNAME: MarqueeLabel!
If not so then start over by first adding the custom class to the label and then taking its outlet in the code file.
I Founded even Much Better Solution !
I Combined 2 Different method together
here with this code in swift you can do Marquee with ultimate repeat and very simple ! Just put Your Label Out side of Display View.
UIView.animateWithDuration(8.0, delay:0, options: [.Repeat], animations: {
self.YOURLABELNAME.frame = CGRectMake(self.YOURLABELNAME.frame.origin.x - 500, self.YOURLABELNAME.frame.origin.y - 0, self.YOURLABELNAME.frame.size.width, self.YOURLABELNAME.frame.size.height)
}, completion: nil)
I don't know why I got 2 - ?!???
I tried many things. that doesn't matter anymore...
I found this 5 min ago and thats my answer for using animation instead of marquee. but on iOS marquee is a tough problem !
here is the link that solved my repeating animation problem
I used this code and it Works ! wish to help others too.
on ViewdidLoad :
if let aLabel = self.txtAmarFull {
aLabel.pushTransition(3)
}
on Body:
extension UIView {
func pushTransition(duration:CFTimeInterval) {
let animation:CATransition = CATransition()
animation.timingFunction = CAMediaTimingFunction(name:
kCAMediaTimingFunctionEaseInEaseOut)
animation.type = kCATransitionMoveIn
animation.subtype = kCATransitionFromLeft
animation.duration = duration
animation.repeatCount = 99
self.layer.addAnimation(animation, forKey: kCATransitionPush)
}
my label name is : txtAmarfull