使用可以对其内容进行Marquee的标签非常简单。只需在项目中添加MarqueeLabel pod即可。
迅速:
pod 'MarqueeLabel/Swift'
然后选择要执行Marquee的标签,并在Identity Inspector中为其添加Custom Class MarqueeLabel。
而已。
这是在Label中添加选框的最简单方法。如果您希望在标签内容的最后一个字符和第一个字符之间留出一些间距,则添加Custom Class MarqueeLabel后:
第1步:选择标签。
第2步:转到属性检查器,然后增加fadeLength属性值。将价值10应用于它是公平的。
如果您希望自定义更多,请将自定义类MarqueeLabel添加到Label,然后在您的代码中取出该Label的插座,并按照您希望的方式对其进行自定义。
代码中该Label的出口应如下所示:
@IBOutlet var YOURLABELNAME: MarqueeLabel!
如果不是这样,那么首先将自定义类添加到标签,然后将其出口放在代码文件中。
我创造了更好的解决方案!
我把2种不同的方法结合起来
在这里使用swift中的这个代码,你可以做Marquee与终极重复,非常简单!只需将显示视图中的标签输出一侧。
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)
检查此代码
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)
}
我不知道为什么我得到2 - ?!???
我尝试了很多东西。那不重要了......
我在5分钟前找到了这个,这就是我使用动画而不是选框的答案。但在iOS选框上是一个棘手的问题!
here is the link that solved my repeating animation problem
我使用这个代码,它的工作原理!也希望帮助别人。
在ViewdidLoad上:
if let aLabel = self.txtAmarFull {
aLabel.pushTransition(3)
}
在身体上:
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)
}
我的标签名称是:txtAmarfull