swift - 渐变色圆形loading

Gradient-circle-LoadingView

先看几张图片


swift - 渐变色圆形loading_第1张图片
Gradient-circle-1.png
swift - 渐变色圆形loading_第2张图片
Gradient-circle-2.png
swift - 渐变色圆形loading_第3张图片
Gradient-circle-3.png
渐变圆loading.gif

//

//GMView.swift

//图层拼接渐变线

//

//Created by Xinxibin on 16/8/8.

//Copyright © 2016年GloryMan. All rights reserved.

//

importUIKit

classGMView:UIView{

//主layer

varshapeLayer:CAShapeLayer!

//圆形layer

varlayer1:CAShapeLayer!

//色卡1

varcolors1:[CGColor] = [UIColor.init(red:137.0/255.0, green:137.0/255.0, blue:137.0/255.0, alpha:1).CGColor,UIColor.blackColor().CGColor]

//色卡2

varcolors2:[CGColor] = [UIColor.init(red:110/255.0, green:110/255.0, blue:110/255.0, alpha:1).CGColor,UIColor.whiteColor().CGColor]

varstrokeEndFloat:CGFloat=0{

didSet{

layer1.strokeEnd=strokeEndFloat

}

}

overridefuncawakeFromNib() {

super.awakeFromNib()

setUI()

}

funcsetUI() {

shapeLayer=CAShapeLayer()

shapeLayer.frame=self.bounds

self.layer.addSublayer(shapeLayer)

//创建梯形layer

letleftLayer =CAGradientLayer()

leftLayer.frame=CGRectMake(0,0,240,120)

leftLayer.colors=colors1

leftLayer.startPoint=CGPointMake(0,0.5)

leftLayer.endPoint=CGPointMake(1,0.5)

shapeLayer.addSublayer(leftLayer)

letrightLayer =CAGradientLayer()

rightLayer.frame=CGRectMake(0,120,240,120)

rightLayer.colors=colors2

rightLayer.startPoint=CGPointMake(0,0.5)

rightLayer.endPoint=CGPointMake(1,0.5)

shapeLayer.addSublayer(rightLayer)

//创建一个圆形layer

layer1=CAShapeLayer()

layer1.frame=self.bounds

layer1.path=UIBezierPath(arcCenter:CGPointMake(120,120), radius:100, startAngle:CGFloat(M_PI/30), endAngle:2*CGFloat(M_PI) -CGFloat(M_PI/30), clockwise:true).CGPath

layer1.lineWidth=5

layer1.lineCap=kCALineCapRound

layer1.lineJoin=kCALineJoinRound

layer1.strokeColor=UIColor.blackColor().CGColor

layer1.fillColor=UIColor.clearColor().CGColor

//根据laery1的layer形状在shaperLayer中截取出来一个layer

shapeLayer.mask=layer1

startLoading()

}

/**

旋转动画

*/

funcstartLoading() {

letanimation =CABasicAnimation(keyPath:"transform.rotation.z")

animation.toValue=2*M_PI

animation.duration=1.25

animation.repeatCount=HUGE

self.layer.addAnimation(animation, forKey:"")

}

}

就是这么简单是不是,下面是我的github,欢迎来 star

Github
博客地址

你可能感兴趣的:(swift - 渐变色圆形loading)