Welcome to the Personal Technology blog station
Create a view that inherits from UIView we call him Xtactivityview.
/// 立方柱的个数 var0; /// 立方柱的颜色 var rectBackgroundColor: UIColor? /// 立方柱初始化大小 var defaultSize: CGSize? /// 立方柱之间的间距 var0.0
To initialize
CGRect) { super.init(frame: frame) self.createDefaultAttribute(frame) }
CGRect) -> Void { 6; self.rectBackgroundColorUIColor.blackColor() 1; defaultSize = frame.size }
Provides start and end interfaces (that is, create and remove)
func startAnimation() { self.addRect() }func stopAnimation() { self.removeRect() }
Add a rectangle
Func addrect (), Void {self. Removerect() Self. Hidden= False For Iinch 0... Numberofrect{Letx= (cgfloat) (i) * (5+ space) Let Rview = UIView. Init(Frame:cgrectmake (x,0,5, defaultsize!. Height)) Rview. BackgroundColor= Rectbackgroundcolor Rview. Layer. Addanimation(Self. Addanimatewithdelay((Double) (i) *0.2), Forkey:"Tbrotate") Self. Addsubview(Rview)} }
Animation effects
Func Addanimatewithdelay (delay:double), caanimation {Let animation = Cabasicanimation. Init(KeyPath:"Transform.rotation.x") animation. RepeatCount= Maxfloat;Animation. Removedoncompletion= True;Animation. Autoreverses= False;Animation. Timingfunction= Camediatimingfunction. Init(name:kcamediatimingfunctionlinear) Animation. Fromvalue= NSNumber. Init(Float:0) animation. Tovalue= NSNumber. Init(DOUBLE:M_PI) Animation. Duration= (Double) (numberofrect) *0.2;Animation. BeginTime= Cacurrentmediatime () + delay;return animation;}
Remove Rectangle
-> Void { ifself.subviews.>0 { self.removeFromSuperview() } self.=true }
Called in the Controller
self.view.backgroundColor = UIColor.whiteColor()let xt = XTActivityView.init(frame: CGRectMake(0108015))xt.startAnimation()self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: xt)xt.performSelector(Selector("stopAnimation"5)
Everyone, I'm the demo address.
Welcome to the Personal Technology blog station
Swift version imitation NetEase cloud music play Music animation effect