class MarqueeView: UIView {
var iconImageView:UIImageView!
var titleLabel:UILabel!
var showLabel:UILabel!
var showTextArray:[String] = ["测试跑马灯1","测试跑马灯2","测试跑马灯3","测试跑马灯4"]
var showTextCount = 0
var showtime:Timer?
override init(frame: CGRect) {
super.init(frame: frame)
let icon_W = frame.height*0.4444
self.iconImageView = UIImageView(frame: CGRect(x: interval_W_20, y: 0, width: icon_W, height: icon_W))
self.iconImageView.frame.origin.y = frame.height * 0.2777
self.addSubview(self.iconImageView)
self.titleLabel = UILabel()
self.titleLabel.font = GlobalFont_32
self.titleLabel.textColor = GlobalMainToneColor_2_4
self.addSubview(self.titleLabel)
self.showLabel = UILabel()
self.showLabel.font = GlobalFont_32
self.showLabel.textColor = GlobalTxtColor_1
self.addSubview(self.showLabel)
self.layer.masksToBounds = true
}
func setData(icon:UIImage,title:String,showTextArr:[String],backColor:UIColor = UIColor.white) {
self.backgroundColor = backColor
self.iconImageView.image = icon
self.titleLabel.text = title.truncate(start: 0, end: 8)
self.titleLabel.sizeToFit()
self.titleLabel.frame.origin.x = self.iconImageView.frame.origin.x+self.iconImageView.frame.width+interval_W_20
self.titleLabel.center.y = self.iconImageView.center.y
self.showLabel.frame.origin.x = self.titleLabel.frame.origin.x+self.titleLabel.frame.width + interval_W_20
if self.showTextArray != showTextArr{
self.showTextArray = showTextArr
self.showTextCount = 0
}
if showtime != nil{
showtime!.invalidate()
showtime = nil
}
self.timeChange()
self.showtime = Timer.scheduledTimer(timeInterval: timeInterval, target: self, selector: #selector(self.timeChange), userInfo: nil, repeats: true)
}
deinit {
printLog("deinit MarqueeView")
}
func clear(){
self.stopTime()
}
private func stopTime(){
if showtime != nil{
showtime!.invalidate()
showtime = nil
}
}
let timeInterval:Double = 5
func timeChange(){
if self.showTextCount < self.showTextArray.count{
self.showLabel.text = self.showTextArray[self.showTextCount]
self.showTextCount += 1
}else{
self.showTextCount = 0
self.showLabel.text = self.showTextArray[self.showTextCount]
}
self.showLabel.sizeToFit()
var frame = self.showLabel.frame
frame.origin.y = self.frame.height
self.showLabel.frame = frame
UIView.beginAnimations("showText", context: nil)
UIView.setAnimationDuration(timeInterval)
UIView.setAnimationCurve(UIViewAnimationCurve.linear)
UIView.setAnimationDelegate(self)
UIView.setAnimationRepeatAutoreverses(false)
UIView.setAnimationRepeatCount(0)
frame = self.showLabel.frame
frame.origin.y = -frame.size.height
self.showLabel.frame = frame
UIView.commitAnimations()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}