SimpleAnimation和UIBezierPath
SimpleAnimation
@objc func rightButtonItem() -> Void{
UIView.animate(withDuration: 1.0) {
self.animationView.frame = CGRect(x: self.view.frame.width / 2 - 50, y: 100, width: 100, height: 100)
self.animationView.alpha = 1.0
self.animationView.backgroundColor = UIColor.red
}
}
<!--動(dòng)畫改變視圖的x,y,width,height-->
func animation(x: CGFloat,y: CGFloat, width: CGFloat, height: CGFloat) -> Void {
UIView.animate(withDuration: 1.0) {
var rect = self.animationView.frame
rect.origin.x += x
rect.origin.y += y
rect.size.width += width
rect.size.height += height
self.animationView.frame = rect
};
}
<!--動(dòng)畫改變視圖的alpha-->
func animationAlpha(alpha: CGFloat) -> Void {
UIView.animate(withDuration: 1.0, animations: {
self.animationView.alpha = alpha
}) { (success) in
// alpha變0 就要去移除view
// self.animationView.removeFromSuperview()
}
}
<!--動(dòng)畫改變視圖的color-->
func animationBackgroundColor(color: UIColor) -> Void {
UIView.animate(withDuration: 1.0) {
self.animationView.backgroundColor = color
}
}
keyFrameAnimation
<!--扇頁旋轉(zhuǎn)-->
func drawAnimation() -> Void {
let animation = CABasicAnimation.init(keyPath: "transform.rotation.z")
animation.fromValue = 0
animation.toValue = CGFloat(Double.pi * 2)
animation.duration = 3.0
animation.autoreverses = false;
animation.fillMode = CAMediaTimingFillMode.forwards;
animation.repeatCount = MAXFLOAT;
self.ellipseView.layer.add(animation, forKey: nil)
}
<!--弧形動(dòng)效-->
func animationPosition(value: CGFloat) -> Void {
// 添加動(dòng)畫
baseAnimation = CABasicAnimation(keyPath: "strokeEnd")
baseAnimation.duration = 2 //持續(xù)時(shí)間
baseAnimation.fromValue = 0 //開始值
baseAnimation.toValue = value //結(jié)束值
// 保持運(yùn)動(dòng)后的位置不變
baseAnimation.isRemovedOnCompletion = false
baseAnimation.fillMode = CAMediaTimingFillMode.forwards
animationView.shapeLayer.add(baseAnimation, forKey: "strokeEndAnimation")
}
圓弧動(dòng)效
let bezierPath = UIBezierPath.init()
let shapeLayer = CAShapeLayer.init()
<!--有些屬性必須要在draw 方法里才能生效-->
override func draw(_ rect: CGRect) {
// Drawing code
let radius = rect.width / 2.0 - 20 //確定半徑
// 繪制圓弧
bezierPath.addArc(withCenter: CGPoint(x: 100, y: 100), radius: radius, startAngle: CGFloat(0.25 + Double.pi / 2.0), endAngle:CGFloat(2 * Double.pi + Double.pi / 2.0 - 0.25), clockwise: true)
bezierPath.lineWidth = 5
UIColor.red.setStroke() // 設(shè)置路徑顏色
bezierPath.stroke()
// 繪制添加動(dòng)畫路徑蒙層
shapeLayer.lineWidth = 5
shapeLayer.strokeColor = UIColor.green.cgColor
shapeLayer.fillColor = UIColor.white.cgColor
shapeLayer.path = bezierPath.cgPath
layer.addSublayer(shapeLayer)
}
扇頁動(dòng)效
override func draw(_ rect: CGRect) {
// 繪制中心原點(diǎn)
let circleBezier = UIBezierPath.init()
circleBezier.addArc(withCenter: CGPoint(x: 100, y: 100), radius: 5, startAngle: 0, endAngle: CGFloat(Double.pi * 2), clockwise: true)
UIColor.green.setFill()
circleBezier.close()
circleBezier.lineWidth = 1
circleBezier.fill()
// 繪制扇葉
let ellipseView = UIView.init(frame: CGRect(x: 0, y: 0, width: 80, height: 5))
ellipseView.center = CGPoint(x: 100, y: 40)
ellipseView.backgroundColor = UIColor.white
self.addSubview(ellipseView)
let ellipseView2 = UIView.init(frame: CGRect(x: 0, y: 0, width: 80, height: 5))
ellipseView2.center = CGPoint(x: 50, y: (50 + sqrt(7500)))
ellipseView2.backgroundColor = UIColor.white
self.addSubview(ellipseView2)
let ellipseView3 = UIView.init(frame: CGRect(x: 0, y: 0, width: 80, height: 5))
ellipseView3.center = CGPoint(x: 150, y: (50 + sqrt(7500)))
ellipseView3.backgroundColor = UIColor.white
self.addSubview(ellipseView3)
let path = UIBezierPath.init(ovalIn:ellipseView.bounds)
let pathLayer = CAShapeLayer.init()
pathLayer.lineWidth = 1
pathLayer.strokeColor = UIColor.green.cgColor
pathLayer.path = path.cgPath
pathLayer.fillColor = nil
ellipseView.layer.addSublayer(pathLayer)
let path2 = UIBezierPath.init(ovalIn:ellipseView2.bounds)
let pathLayer2 = CAShapeLayer.init()
pathLayer2.lineWidth = 1
pathLayer2.strokeColor = UIColor.green.cgColor
pathLayer2.path = path2.cgPath
pathLayer2.fillColor = nil
ellipseView2.layer.addSublayer(pathLayer2)
let path3 = UIBezierPath.init(ovalIn:ellipseView3.bounds)
let pathLayer3 = CAShapeLayer.init()
pathLayer3.lineWidth = 1
pathLayer3.strokeColor = UIColor.green.cgColor
pathLayer3.path = path3.cgPath
pathLayer3.fillColor = nil
ellipseView3.layer.addSublayer(pathLayer3)
UIView.animate(withDuration: 0) {
ellipseView.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2.0))
ellipseView2.transform = CGAffineTransform(rotationAngle: CGFloat(-Double.pi / 4.0 + (1 / 12)))
ellipseView3.transform = CGAffineTransform(rotationAngle: CGFloat(-Double.pi / 4.0 * 3 - (1 / 12)))
}
}
扇形
override func draw(_ rect: CGRect) {
// Drawing code
let circleBezier = UIBezierPath.init()
circleBezier.addArc(withCenter: CGPoint(x: 100, y: 100), radius: 100, startAngle: CGFloat(Double.pi / 2.0 - 0.1), endAngle: CGFloat(Double.pi / 2.0 + 0.1), clockwise: true)
UIColor.green.setStroke()
circleBezier.addLine(to: CGPoint(x: 100, y: 100))
circleBezier.close()
circleBezier.lineWidth = 1
circleBezier.stroke()
}
直通車: https://github.com/princeSmall/Swift_Animations