- 先来看看效果
- 首先重载UIButton,重载不用写override
init(frame: CGRect, backgroundColor: UIColor) {
super.init(frame: frame)
self.backgroundColor = backgroundColor
self.setTitle("立即付款", for: UIControlState.normal)
self.setTitle("面容ID支付处理中", for: UIControlState.disabled)
self.addTarget(self, action: #selector(payAction), for: UIControlEvents.touchUpInside)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
fatalError("")
} - 在点击事件中启动draw方法,self.setNeedsDisplay()
- 在draw方法中实现loading动画效果
override func draw(_ rect: CGRect) {
if !self.isEnabled {
let frame = self.titleLabel!.frame
let x = frame.origin.x-frame.size.height*1.3
let y = frame.origin.y+frame.size.height/2
//在button的title左边画一个圆形路径
//clockwise: true,顺时针
let path = UIBezierPath(arcCenter: CGPoint(x: x, y: y), radius: frame.size.height*0.6, startAngle: -CGFloat.pi/2, endAngle:CGFloat.pi*2, clockwise: true)
path.lineWidth = 2
//根据上述路径建立图层
let layer = CAShapeLayer()
layer.path = path.cgPath
layer.strokeColor = UIColor.white.cgColor
layer.fillColor = UIColor.clear.cgColor
self.layer.addSublayer(layer)
//重复画十次
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.fromValue = 0
animation.toValue = 1.0
animation.duration = 1.0
animation.isRemovedOnCompletion = false//设置动画结束后移除
animation.repeatCount = 10
animation.delegate = self//实现代理,在动画结束后改变title
animation.fillMode = kCAFillModeForwards//动画执行完毕会停留在动画结束的位置
layer.add(animation, forKey: "RotationAnimation")
}
} - CABasicAnimation的keyPath值,strokeEnd和strokenStart分析
stroke / fromValue -> toValue start(默认end=1)end(默认start=1)0 -> 1 从头到尾消失 从头到尾出现 1 -> 0 从尾到头出现 从尾到头消失
模仿支付宝支付按钮动画效果——swift
猜你喜欢
转载自blog.csdn.net/weixin_42012181/article/details/80078486
今日推荐
周排行