Swift-calayer Animation of Contents properties
Effect
Source
Https://github.com/YouXianMing/Swift-Animations
////Liveimageview.swift//swift-animations////Created by youxianming on 16/8/17.//copyright©2016 year youxianming. All rights reserved.//Import UIKit//mark:public Class:liveimageviewclassLiveimageview:uiimageview {//mark:properties. ///Animation ' s duration.var duration:cftimeinterval =0.3 //Mark:methods. /** Set image with animation or not. -Parameter newval:the new image. -Parameter animated:animated or not. */func setimage (newval:uiimage, animated:bool) {ifAnimated = =true{Let animation= Cabasicanimation (keypath:"Contents") Animation.fromvalue= image?. Cgimage Animation.tovalue=newval.cgimage animation.duration=Duration player.contents= image?. Cgimage player.addanimation (animation, forkey:nil) image=newval}Else{image=newval}} //mark:private Value & func. Privatevar player:calayer!OverrideInit (frame:cgrect) {super.init (frame:frame) PLayer=Layer} required init?(coder Adecoder:nscoder) {fatalerror ("Init (coder:) has not been implemented") }}
////Liveimageviewcontroller.swift//swift-animations////Created by youxianming on 16/8/17.//copyright©2016 year youxianming. All rights reserved.//Import UIKitclassLiveimageviewcontroller:normaltitleviewcontroller {var timer:gcdtimer! =Gcdtimer (InQueue:GCDQueue.mainQueue) var count:nsinteger! =0var images: [UIImage]! =[UIImage] ()Overridefunc Setup () {Super.setup () images=[UIImage] () images.append (UIImage (named:"pic_1")!) Images.append (UIImage (named:"pic_2")!) Images.append (UIImage (named:"Pic_3")!) Images.append (UIImage (named:"Pic_4")!) Let image= images[0] Let Liveimageview= Liveimageview (Frame:cgrectmake (0,0, Image.size.width, Image.size.height)) Liveimageview.center= (Contentview?. Middlepoint)!LiveImageView.layer.borderWidth=3LiveImageView.layer.borderColor=Uicolor.blackcolor (). Cgcolor liveimageview.duration=0.5Contentview?. Addsubview (Liveimageview) weak var wself=Self timer.Event({Let currentindex= (wself?. Count)! % (wself?. Images.count)!wself?. Count = (wself?. Count)! +1Liveimageview.setimage (wself!. Images[currentindex], animated:true) uiview.animatewithduration (0.5, animations: {var tmprect=liveimageview.bounds tmprect.size= (liveimageview.image?. Size)!Liveimageview.bounds=Tmprect Liveimageview.center= (wself?. Contentview?. Middlepoint)!})}, Timeintervalwithseconds:1.0) Timer.start ()}}
Swift-calayer Animation of Contents properties