swift中的动画
2018-05-24 本文已影响0人
陈藩
尝试用swift写简单动画。
CABasicAnimation 和CAKeyframeAnimation 的keyPath 都是CALayer的属性或者间接属性,比如"position","position.x",类似的还有backGroundColor,alpha,bounds,borderWidth,只要看CALayer的文档就知道了,因为他们都是继承自CAPropertyAnimation的。而其中复杂的动画也是简单的动画合成的。
func aniation0() {
let aniamtion = CABasicAnimation.init(keyPath: "position.x")
aniamtion.toValue = UIScreen.main.bounds.size.width
aniamtion.duration = 3.0
aniamtion.repeatCount = 10;
aniamtion.autoreverses = false
self.imgeView?.layer.add(aniamtion, forKey: "alpha")
}
func aniation1() {
let aniamtion = CABasicAnimation.init(keyPath: "bounds")
aniamtion.fromValue = CGRect.init(origin: CGPoint.init(x: 0, y: 64), size: CGSize.init(width: 240, height: 240))
aniamtion.toValue = CGRect.init(origin: CGPoint.init(x: 0, y: 64), size: CGSize.init(width: 100, height: 100))
aniamtion.duration = 3.0;
aniamtion.repeatCount = 10;
aniamtion.autoreverses = false;
self.imgeView?.layer.add(aniamtion, forKey: "bounds");
}
func aniation2() {
let aniamtion = CABasicAnimation.init(keyPath: "bounds.size")
aniamtion.fromValue = CGSize.init(width: 240, height: 240)
aniamtion.toValue = CGSize.init(width: 100, height: 100)
aniamtion.duration = 3.0;
aniamtion.repeatCount = 10;
aniamtion.autoreverses = false;
self.imgeView?.layer.add(aniamtion, forKey: "bounds.size");
}
func aniation3() {
let aniamtion = CABasicAnimation.init(keyPath: "anchorPoint") //取值范围在(0,0)(1,1)
aniamtion.fromValue = CGPoint.init(x: 0.5, y: 0.5)
aniamtion.toValue = CGPoint.init(x:0.0, y:0.0);
aniamtion.duration = 3.0;
aniamtion.repeatCount = 2;
aniamtion.autoreverses = false;
self.imgeView?.layer.add(aniamtion, forKey:"anchorPoint");
}
func aniation4() {
let aniamtion = CABasicAnimation.init(keyPath: "backgroundColor")
aniamtion.fromValue = UIColor.red.cgColor
aniamtion.toValue = UIColor.green.cgColor
aniamtion.duration = 3.0;
aniamtion.repeatCount = 2;
aniamtion.autoreverses = false;
self.imgeView?.layer.add(aniamtion, forKey:"backgroundColor");
}
func aniation5() {
let aniamtion = CABasicAnimation.init(keyPath: "cornerRadius") //取值范围在(0,0)(1,1)
aniamtion.fromValue = 0
aniamtion.toValue = 120
aniamtion.duration = 3.0;
aniamtion.repeatCount = 2;
aniamtion.autoreverses = false;
self.imgeView?.layer.add(aniamtion, forKey:"cornerRadius");
}
func aniation6() {
let aniamtion = CABasicAnimation.init(keyPath: "borderWidth") //取值范围在(0,0)(1,1)
aniamtion.fromValue = 0
aniamtion.toValue = 10
aniamtion.duration = 3.0;
aniamtion.repeatCount = 2;
aniamtion.autoreverses = false;
self.imgeView?.layer.add(aniamtion, forKey:"borderWidth");
}
func aniation7() {
let aniamtion = CABasicAnimation.init(keyPath: "borderColor") //取值范围在(0,0)(1,1)
aniamtion.fromValue = UIColor.green.cgColor
aniamtion.toValue = UIColor.blue.cgColor
aniamtion.duration = 3.0;
aniamtion.repeatCount = 2;
aniamtion.autoreverses = false;
self.imgeView?.layer.borderWidth = 5;
self.imgeView?.layer.add(aniamtion, forKey:"borderColor");
}
func aniation8() {
let aniamtion = CABasicAnimation.init(keyPath: "opacity") //取值范围在 0 1 之间
aniamtion.fromValue = 0
aniamtion.toValue = 1.0
aniamtion.duration = 3.0;
aniamtion.repeatCount = 2;
aniamtion.autoreverses = false;
self.imgeView?.layer.add(aniamtion, forKey:"opacity");
}
func aniation9() {
self.aniation0()
self.aniation1()
}
func aniation10() {
self.aniation1()
self.aniation2()
}
func aniation11() {
self.aniation2()
self.aniation3()
}
func aniation12() {
self.aniation3()
self.aniation4()
}
func aniation13() {
self.aniation4()
self.aniation5()
}
func aniation14() {
self.aniation5()
self.aniation6()
}
func aniation15() {
self.aniation6()
self.aniation7()
self.aniation8()
}
下面是UIView动画和Keyframe动画
func aniation0(){
let aniamtion = CAKeyframeAnimation.init(keyPath: "bounds")
aniamtion.values = [CGRect.init(x: 0, y: 64, width: 240, height: 240),
CGRect.init(x: 0, y: 64, width: 250, height: 250),
CGRect.init(x: 0, y: 64, width: 260, height: 260),
CGRect.init(x: 0, y: 64, width: 270, height: 270),
CGRect.init(x: 0, y: 64, width: 280, height: 280),
CGRect.init(x: 0, y: 64, width: 290, height: 290),
CGRect.init(x: 0, y: 64, width: 300, height: 300)
]
aniamtion.timingFunctions = [CAMediaTimingFunction.init(name: kCAMediaTimingFunctionEaseOut)];
aniamtion.duration = 3;
aniamtion.calculationMode = kCAAnimationLinear
self.imgeView?.layer.add(aniamtion, forKey: "bounds")
}
func aniation1(){
UIView.animate(withDuration: 2.0) {
self.imgeView?.frame = CGRect.init(x: 0, y: 64, width: 100, height: 100)
}
}
func aniation2(){
UIView.animate(withDuration: 2.0, animations: {
self.imgeView?.backgroundColor = UIColor.green
}, completion: { (complete) in
self.imgeView?.frame = CGRect.init(x: 0, y: 64, width: 300, height: 300)
})
}
func aniation3(){
UIView.animate(withDuration: 2.0, animations: {
self.imgeView?.backgroundColor = UIColor.green
}, completion: { (complete) in
self.imgeView?.frame = CGRect.init(x: 0, y: 64, width: 300, height: 300)
})
}
func aniation4(){
UIView.animate(withDuration: 2.0, delay: 3.0, options: UIViewAnimationOptions.autoreverse, animations: {
self.imgeView?.frame = CGRect.init(x: 0, y:64, width:100, height:100)
}) { (true) in
self.imgeView?.backgroundColor = UIColor.yellow
}
}
func aniation5(){
self.imgeView?.frame = CGRect.init(x: 0, y: 64, width: 50, height: 50)
self.imgeView?.layer.cornerRadius = 25;
self.imgeView?.layer.masksToBounds = true
let animation = CAKeyframeAnimation.init(keyPath:"position")
animation.values = [CGRect.init(origin: CGPoint.init(x: 20, y: 64 + 10), size: (self.imgeView?.frame.size)!),
CGRect.init(origin: CGPoint.init(x: 30, y: 64 + 20), size: (self.imgeView?.frame.size)!),
CGRect.init(origin: CGPoint.init(x: 40, y: 64 + 30), size: (self.imgeView?.frame.size)!),
CGRect.init(origin: CGPoint.init(x: 50, y: 64 + 40), size: (self.imgeView?.frame.size)!),
CGRect.init(origin: CGPoint.init(x: 60, y: 64 + 50), size: (self.imgeView?.frame.size)!),
CGRect.init(origin: CGPoint.init(x: 70, y: 64 + 60), size: (self.imgeView?.frame.size)!),
CGRect.init(origin: CGPoint.init(x: 80, y: 64 + 70), size: (self.imgeView?.frame.size)!),
CGRect.init(origin: CGPoint.init(x: 90, y: 64 + 80), size: (self.imgeView?.frame.size)!),
CGRect.init(origin: CGPoint.init(x: 100, y: 64 + 90), size: (self.imgeView?.frame.size)!),
CGRect.init(origin: CGPoint.init(x: 110, y: 64 + 100), size: (self.imgeView?.frame.size)!),
CGRect.init(origin: CGPoint.init(x: 120, y: 64 + 110), size: (self.imgeView?.frame.size)!),
CGRect.init(origin: CGPoint.init(x: 130, y: 64 + 120), size: (self.imgeView?.frame.size)!)
]
animation.duration = 10;
animation.fillMode = kCAFillModeForwards
animation.autoreverses = false
self.imgeView?.layer.add(animation, forKey: "position")
}
缩放动画
+(void )aniamtionWithView:(UIView *)view{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.duration = 0.4;
animation.repeatCount = 1;
animation.autoreverses = YES;
// 缩放倍数
animation.fromValue = [NSNumber numberWithFloat:1.0];
animation.toValue = [NSNumber numberWithFloat:1.2];
// 添加动画
[view.layer addAnimation:animation forKey:@"scale-layer"];
}