import UIKit
class CyclicScrollingADView : UIView {
varfirstLabel :UILabel?
varsecondLabel :UILabel?
varcurrentIndex :Int=0
varstate :Bool=true
lazyvartimer :Timer= {
lettimer =Timer.init(timeInterval:4, target:self, selector:#selector(CyclicScrollingADView.timeChange), userInfo:nil, repeats:true)
RunLoop.current.add(timer, forMode:RunLoopMode.commonModes)
returntimer;
}()
vardataSource : [NSAttributedString] = [] {
didSet{
firstLabel?.attributedText=dataSource[0]
currentIndex=0
self.timer.fire()
}
}
overrideinit(frame:CGRect) {
super.init(frame: frame)
self.clipsToBounds=true
firstLabel=UILabel.init(frame:CGRect.init(x:0, y:0, width: frame.size.width, height: frame.size.height))
firstLabel?.textColor=UIColor.white
firstLabel?.font=UIFont.boldSystemFont(ofSize:11)
secondLabel=UILabel.init(frame:CGRect.init(x:0, y: frame.size.height, width: frame.size.width, height: frame.size.height))
secondLabel?.textColor=UIColor.white
secondLabel?.font=UIFont.boldSystemFont(ofSize:11)
self.addSubview(firstLabel!)
self.addSubview(secondLabel!)
}
@objcfunctimeChange(){
currentIndex=currentIndex+1
ifcurrentIndex>=dataSource.count{
currentIndex=0
}
ifstate{
//展示第二个
self.secondLabel?.attributedText=self.dataSource[currentIndex]
UIView.animate(withDuration:0.5, animations: {
self.firstLabel?.frame=CGRect.init(x:0, y: -self.frame.size.height, width:self.frame.size.width, height:self.frame.size.height)
self.secondLabel?.frame=CGRect.init(x:0, y:0, width:self.frame.size.width, height:self.frame.size.height)
}) { (finish)in
self.firstLabel?.frame=CGRect.init(x:0, y:self.frame.size.height, width:self.frame.size.width, height:self.frame.size.height)
}
}else{
//展示第一个
self.firstLabel?.attributedText=self.dataSource[currentIndex]
UIView.animate(withDuration:0.5, animations: {
self.secondLabel?.frame=CGRect.init(x:0, y: -self.frame.size.height, width:self.frame.size.width, height:self.frame.size.height)
self.firstLabel?.frame=CGRect.init(x:0, y:0, width:self.frame.size.width, height:self.frame.size.height)
}) { (finish)in
self.secondLabel?.frame=CGRect.init(x:0, y:self.frame.size.height, width:self.frame.size.width, height:self.frame.size.height)
}
}
state= !state
}
requiredinit?(coder aDecoder:NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
网友评论