固定大小淡入淡出的效果:
#import <QuartzCore/QuartzCore.h>
@implementation TrendLineCircleDot
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
[self.layer addAnimation:[self opacityForever_Animation:0.8] forKey:nil];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(ctx, rect);
[WIND_COLOR(STOCK_INFO_F5_GRAPH_TREND_LINE) set];
CGContextFillPath(ctx);
}
- (CABasicAnimation *)opacityForever_Animation:(float)time
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; //必须写opacity才行。
animation.fromValue = [NSNumber numberWithFloat:1.0f];
animation.toValue = [NSNumber numberWithFloat:0.2f];
animation.autoreverses = YES;
animation.duration = time;
animation.repeatCount = MAXFLOAT;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
return animation;
}
@end
扩大的效果:
- (CALayer *)flashPointLayer {
if(!_flashPointLayer) {
_flashPointLayer = [[CALayer alloc] init];
CGFloat width = 20;
if (KDS_FSViewType_Horizontal == _fsViewType) {
width = 20;
}
_flashPointLayer.bounds = CGRectMake(0, 0, width, width);
_flashPointLayer.cornerRadius = width/2;
UIColor *color = skinColor(@"FSKLine_Color_FSCJJGLine");
_flashPointLayer.backgroundColor = color.CGColor;
[self setupAnimationInLayer:_flashPointLayer
withSize:CGSizeMake(15, 15)
tintColor:color];
}
return _flashPointLayer;
}
- (void)setupAnimationInLayer:(CALayer *)layer withSize:(CGSize)size tintColor:(UIColor *)tintColor {
NSTimeInterval beginTime = CACurrentMediaTime();
CGFloat oX = (layer.bounds.size.width - size.width) / 2.0f;
CGFloat oY = (layer.bounds.size.height - size.height) / 2.0f;
for (int i = 0; i < 2; i++) {
CALayer *circle = [CALayer layer];
circle.frame = CGRectMake(oX, oY, size.width, size.height);
circle.backgroundColor = tintColor.CGColor;
circle.anchorPoint = CGPointMake(0.5f, 0.5f);
circle.opacity = 0.8f;
circle.cornerRadius = circle.bounds.size.height / 2.0f;
circle.transform = CATransform3DMakeScale(0.0f, 0.0f, 0.0f);
CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
transformAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.0f, 0.0f, 0.0f)];
transformAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0f, 1.0f, 0.0f)];
CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.fromValue = @(0.8f);
opacityAnimation.toValue = @(0.0f);
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.removedOnCompletion = NO;
animationGroup.beginTime = beginTime + i * 0.2f;
animationGroup.repeatCount = HUGE_VALF;
animationGroup.duration = 1.2f;
animationGroup.animations = @[transformAnimation, opacityAnimation];
[layer addSublayer:circle];
[circle addAnimation:animationGroup forKey:@"animation"];
}
}