首先看下效果展示:
Core Animation(核心動畫)是一組功能強大裂垦、效果華麗的動畫API寺枉,無論在iOS系統(tǒng)或者在你開發(fā)的App中括蝠,都有大量應(yīng)用来候。
樓主簡單的利用UIBezierPath
繪畫圓,然后利用CABasicAnimation
的opacity
調(diào)整視圖的透明度左敌,然后利用CABasicAnimation
的transform.scale
來實現(xiàn)圓的放大縮小瘾蛋,如果想要了解更多傳送門:
http://www.imlifengfeng.com/blog/?p=548
廢話不多所上代碼
基本都是通過懶加載實現(xiàn)
創(chuàng)建CAShapeLayer
- (CAShapeLayer *)shapeLayer{
if (_shapeLayer==nil) {
_shapeLayer = [CAShapeLayer layer];
_shapeLayer.frame = CGRectMake(100, 100, 100, 100);
_shapeLayer.fillColor = [UIColor blueColor].CGColor;
_shapeLayer.strokeColor = [UIColor blackColor].CGColor;
//通過貝塞爾曲線繪制圓
CGFloat startAngle = 0.0;
CGFloat endAngle = M_PI *2;
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(50, 50) radius:50 startAngle:startAngle endAngle:endAngle clockwise:YES];
_shapeLayer.path = bezierPath.CGPath;
}
return _shapeLayer;
}
添加動畫組這里用到CAAnimationGroup
- (CAAnimationGroup *)animaGroup{
if (_animaGroup == nil) {
CABasicAnimation * _opacityAnima = [CABasicAnimation animationWithKeyPath:@"opacity"];
_opacityAnima.fromValue = @(0.7);
_opacityAnima.toValue = @(0.3);
CABasicAnimation *expandAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
expandAnimation.fromValue = [NSNumber numberWithFloat:1]; // 開始時的倍率
expandAnimation.toValue = [NSNumber numberWithFloat:1.5]; // 結(jié)束時的倍率
_animaGroup = [CAAnimationGroup animation];
_animaGroup.animations = @[ expandAnimation,_opacityAnima];
_animaGroup.duration = 3;
_animaGroup.repeatCount = HUGE;
_animaGroup.autoreverses = YES;
}
return _animaGroup;
}
具體里面的屬性可以點擊上文鏈接了解不多解釋,只實現(xiàn)
動畫開始矫限、停止方法
/Start Animation
- (void)startAnimation{
[self.layer addSublayer:self.shapeLayer];
[self.shapeLayer addAnimation:self.animaGroup forKey:@"scaleGroup"];
}
//Stop Animation
- (void)stopAnimation{
if (_shapeLayer) {
[self.shapeLayer removeAllAnimations];
[self.shapeLayer removeFromSuperlayer];
}
}
代碼地址:https://github.com/lanjiaoli/Animation
有不足的地方大家多多指出哺哼,謝謝