LED marquee effect in iOS
The implementation principle is to use the ScrollView, add the label that needs to scroll two times to ScrollView Subview below, and then scroll scrollview to achieve the marquee effect.
The specific implementation code is as follows:
kmscrolllabel.swift//stopsmokingprograms////Created by Fran on 15/11/2.//copyright©2015 year kimree. All rights reserved.//import uikit//text scrolling direction enum kmscrolldirection:int{Case left = 0 Down}class Kmscrolllabel:uiscrollview {//label is read-only//The text of the label is only modified outside the private var label = UILabel () var textlabel:uilabel{get{return label}}//label is sliding private var keepscroll = False//animation time var animationduringtime:double = 5 deinit{kmlog ("Kmscrolllabel Deinit")} Convenience init () {Self.init (Frame:cgrectzero)} override init (Frame:cgrect) {Super.init (fr Ame:frame) Kmlog ("init frame") self.scrollenabled = False Self.showshorizontalscrollindicato r = False Self.showsverticalscrollindicator = False Self.addsubview (label)} required init? (Coder Adecoder:nscoder) {SUPER.INIT (Coder:adecoder) kmlog ("Init adecoder") self.scrollenabled = False SELF.SHOWSHORIZONTALSC Rollindicator = False Self.showsverticalscrollindicator = False Self.addsubview (label)}//MARK: -Start sliding func startscroll (direction:kmscrolldirection =. left) {fitframe (direction)///When the label is not in the sliding state, you can start sliding, avoiding the animation effect overlay if!keepscroll{ke Epscroll = True Self.contentoffset = Cgpointzero beginscroll (direction)}}//Remo The completion portion of the animation block will be executed immediately after veallanimations,//The parameters of the completion section are not currently determined to be false, in order to avoid coincidences, it is necessary to cooperate with Keepscroll Parameters using Func stopscroll () {Keepscroll = False self.layer.removeAllAnimations ()}//MARK:-How exactly Slide Func beginscroll (direction:kmscrolldirection =. left) {Let w = label.frame.size.width let h = label.frame.size.height switch direction{ Case. Left:self.coNtentoffset.x = 0 uiview.animatewithduration (animationduringtime, delay:0, Options:UIViewAnimationOptions.Cur Velinear, animations: {[weak self] ()-Void in self?. Contentoffset.x = w}, completion: {[weak self] (finished:bool)-Void in if finish Ed && (Self! = nil && self!. Keepscroll) {self!. Contentoffset.x = 0 self!. Beginscroll (Direction)}}) case. Right:self.contentoffset.x = w uiview.animatewithduration (animationduringtime, delay:0, Options:u Iviewanimationoptions.curvelinear, animations: {[weak self] ()-Void in self?. Contentoffset.x = 0}, Completion: {[weak self] (finished:bool)-Void in if finish Ed && (Self! = nil && self!. Keepscroll) {self!. Contentoffset.x = W self!. Beginscroll (Direction)}}) case. Up:self.contentoffset.y = 0 uiview.animatewithduration (animationduringtime, delay:0, Options:uivi Ewanimationoptions.curvelinear, animations: {[weak self] ()-Void in self?. Contentoffset.y = h}, completion: {[weak self] (finished:bool)-Void in if finish Ed && (Self! = nil && self!. Keepscroll) {self!. Contentoffset.y = 0 self!. Beginscroll (Direction)}}) case. Down:self.contentoffset.y = h uiview.animatewithduration (animationduringtime, delay:0, Options:ui Viewanimationoptions.curvelinear, animations: {[weak self] ()-Void in self?. Contentoffset.y = 0}, Completion: {[weak self] (finished:bool), Void in if fInished && (self! = nil && self!. Keepscroll) {self!. Contentoffset.y = h self!. Beginscroll (Direction)}}}}//MARK:-Fit Frame Private F UNC Fitframe (direction:kmscrolldirection) {//Modify frame func fitframeathorizontaldirection () as required for horizontal movement { Let width = self.frame.size.width var height = self.frame.size.height Label.numberoflines = 1 Label.linebreakmode =. bywordwrapping var size = label.sizethatfits (Cgsizemake (Cgfloat.max, height))//Scroll Height cannot be less than the height of the label if height < size.height{self.frame.size.height = Size.Height Height = size.height}//Label width to add half of scroll width as white, and label The width of the scroll cannot be less than the width size.width + = width/2.0 If size.width < width{size.width = width} label.frame = Cgrec Tmake (0, (height-size.height)/2.0, Size.width, size.height) Let Templabel = nskeyedunarchiver.u Narchiveobjectwithdata (nskeyedarchiver.archiveddatawithrootobject (label)) as! UILabel templabel.frame.origin.x + = Size.width self.addsubview (templabel) sel F.contentsize = Cgsizemake (2 * size.width, height)}//Modify frame func Fitframeatver as required to move in a vertical direction Ticaldirection () {var width = self.frame.size.width let height = self.frame.size.height Label.numberoflines = 0 Label.linebreakmode =. bywordwrapping Let Originaltext = label.text//Get the width of single text Label.text = "Tian" Let singlesize = Label.sizethatfits (cgsizezero) Label.text = Originaltext var size = label.sizethatfits (Cgsizemake (Singlesize.width, Cgfloat.max)) if width < size . width{Self.frame.size.width = size.width width = size.width} Size.Height + = height/2.0 if size.height < height{size.height = height} Label.frame = CGRectMake ((width-size.width)/2.0, 0, Size.width, size.height) Let Templabel = Nskeyedunarchiver.unarchiveobjectwithdata (nskeyedarchiver.archiveddatawithrootobject (label)) as! UILabel TEMPLABEL.FRAME.ORIGIN.Y + = Size.Height self.addsubview (templabel) se Lf.contentsize = Cgsizemake (width, 2 * size.height)}//First traverse all subView of scroll, remove the placeholder label For view in self.subviews{if view! = label{View.removefromsuperview ()}} Switch Direction{case. Left,. Right:fitframeathorizontaldirection () case. Up,. Down:fitframeatverticaldirection ()}}/*//Only override drawrect:if you perfor M custom Drawing. An empty implementation adversely affects performance during animation. Override func DrawRect (rect:cgrect) {//Drawing code} */}
IOS led marquee effect achieved