自定义Layer
class WaterWave:CAShapeLayer{
let KAnimationDuration:CFTimeInterval=1
overrideinit() {
super.init()
fillColor=UIColor.greenColor().CGColor
path=wavePathStarting.CGPath
}
requiredinit?(coder aDecoder:NSCoder) {
fatalError("init(coder:) has not been implemented")
}
varwavePathPre:UIBezierPath{
letarcPath =UIBezierPath()
arcPath.moveToPoint(CGPoint(x:0.0, y:100.0))
arcPath.addLineToPoint(CGPoint(x:0.0, y:99.0))
arcPath.addLineToPoint(CGPoint(x:100.0, y:99.0))
arcPath.addLineToPoint(CGPoint(x:100.0, y:100.0))
arcPath.addLineToPoint(CGPoint(x:0.0, y:100.0))
arcPath.closePath()
returnarcPath
}
varwavePathStarting:UIBezierPath{
letarcPath =UIBezierPath()
arcPath.moveToPoint(CGPoint(x:0.0, y:100.0))
arcPath.addLineToPoint(CGPoint(x:0.0, y:80.0))
arcPath.addCurveToPoint(CGPoint(x:100.0, y:80.0), controlPoint1:CGPoint(x:30.0, y:70.0), controlPoint2:CGPoint(x:40.0, y:90.0))
arcPath.addLineToPoint(CGPoint(x:100.0, y:100.0))
arcPath.addLineToPoint(CGPoint(x:0.0, y:100.0))
arcPath.closePath()
returnarcPath
}
varwavePathLow:UIBezierPath{
letarcPath =UIBezierPath()
arcPath.moveToPoint(CGPoint(x:0.0, y:100.0))
arcPath.addLineToPoint(CGPoint(x:0.0, y:60.0))
arcPath.addCurveToPoint(CGPoint(x:100.0, y:60.0), controlPoint1:CGPoint(x:30.0, y:65.0), controlPoint2:CGPoint(x:40.0, y:50.0))
arcPath.addLineToPoint(CGPoint(x:100.0, y:100.0))
arcPath.addLineToPoint(CGPoint(x:0.0, y:100.0))
arcPath.closePath()
returnarcPath
}
varwavePathMid:UIBezierPath{
letarcPath =UIBezierPath()
arcPath.moveToPoint(CGPoint(x:0.0, y:100.0))
arcPath.addLineToPoint(CGPoint(x:0.0, y:40.0))
arcPath.addCurveToPoint(CGPoint(x:100.0, y:40.0), controlPoint1:CGPoint(x:30.0, y:30.0), controlPoint2:CGPoint(x:40.0, y:50.0))
arcPath.addLineToPoint(CGPoint(x:100.0, y:100.0))
arcPath.addLineToPoint(CGPoint(x:0.0, y:100.0))
arcPath.closePath()
returnarcPath
}
varwavePathHigh:UIBezierPath{
letarcPath =UIBezierPath()
arcPath.moveToPoint(CGPoint(x:0.0, y:100.0))
arcPath.addLineToPoint(CGPoint(x:0.0, y:20.0))
arcPath.addCurveToPoint(CGPoint(x:100.0, y:20.0), controlPoint1:CGPoint(x:30.0, y:25.0), controlPoint2:CGPoint(x:40.0, y:10.0))
arcPath.addLineToPoint(CGPoint(x:100.0, y:100.0))
arcPath.addLineToPoint(CGPoint(x:0.0, y:100.0))
arcPath.closePath()
returnarcPath
}
varwavePathComplete:UIBezierPath{
letarcPath =UIBezierPath()
arcPath.moveToPoint(CGPoint(x:0.0, y:100.0))
arcPath.addLineToPoint(CGPoint(x:0.0, y:0))
arcPath.addLineToPoint(CGPoint(x:100.0, y:0))
arcPath.addLineToPoint(CGPoint(x:100.0, y:100.0))
arcPath.addLineToPoint(CGPoint(x:0.0, y:100.0))
arcPath.closePath()
returnarcPath
}
funcanimate() {
/// 1
letwaveAnimationPre:CABasicAnimation=CABasicAnimation(keyPath:"path")
waveAnimationPre.fromValue=wavePathPre.CGPath
waveAnimationPre.toValue=wavePathStarting.CGPath
waveAnimationPre.beginTime=0.0
waveAnimationPre.duration=KAnimationDuration
/// 2
letwaveAnimationLow:CABasicAnimation=CABasicAnimation(keyPath:"path")
waveAnimationLow.fromValue=wavePathStarting.CGPath
waveAnimationLow.toValue=wavePathLow.CGPath
waveAnimationLow.beginTime= waveAnimationPre.beginTime+ waveAnimationPre.duration
waveAnimationLow.duration=KAnimationDuration
/// 3
letwaveAnimationMid:CABasicAnimation=CABasicAnimation(keyPath:"path")
waveAnimationMid.fromValue=wavePathLow.CGPath
waveAnimationMid.toValue=wavePathMid.CGPath
waveAnimationMid.beginTime= waveAnimationLow.beginTime+ waveAnimationLow.duration
waveAnimationMid.duration=KAnimationDuration
/// 4
letwaveAnimationHigh:CABasicAnimation=CABasicAnimation(keyPath:"path")
waveAnimationHigh.fromValue=wavePathMid.CGPath
waveAnimationHigh.toValue=wavePathHigh.CGPath
waveAnimationHigh.beginTime= waveAnimationMid.beginTime+ waveAnimationMid.duration
waveAnimationHigh.duration=KAnimationDuration
/// 5
letwaveAnimationComplete:CABasicAnimation=CABasicAnimation(keyPath:"path")
waveAnimationComplete.fromValue=wavePathHigh.CGPath
waveAnimationComplete.toValue=wavePathComplete.CGPath
waveAnimationComplete.beginTime= waveAnimationHigh.beginTime+ waveAnimationHigh.duration
waveAnimationComplete.duration=KAnimationDuration
/// group animation
letarcAnimationGroup:CAAnimationGroup=CAAnimationGroup()
arcAnimationGroup.animations= [waveAnimationPre, waveAnimationLow, waveAnimationMid, waveAnimationHigh, waveAnimationComplete]
arcAnimationGroup.duration= waveAnimationComplete.beginTime+ waveAnimationComplete.duration
arcAnimationGroup.fillMode=kCAFillModeForwards
arcAnimationGroup.removedOnCompletion=false
addAnimation(arcAnimationGroup, forKey:nil)
}
}
class YLWaterWaveAnimationView:UIView{
let waterWave:WaterWave=WaterWave()
var imageView:UIImageView?
overrideinit(frame:CGRect) {
super.init(frame: frame)
imageView=UIImageView.init(frame: frame)
imageView?.backgroundColor=UIColor.redColor()
imageView?.layer.addSublayer(waterWave)
waterWave.animate()
addSubview(imageView!)
}
requiredinit?(coder aDecoder:NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
ViewController.swift
let wave =YLWaterWaveAnimationView.init(frame:CGRectMake(0,0,100,100))
wave.center=self.view.center
view.addSubview(wave)