iOS Layer CABasicAnimation

    CALayer *layer = [CALayer layer];

    layer.delegate = self;

    layer.bounds = CGRectMake(0, 0, 100, 100);

    layer.position = CGPointMake(100, 100);

    layer.anchorPoint = CGPointZero;

    layer.backgroundColor = [UIColor blackColor].CGColor;

    [layer setNeedsDisplay];

    [self.view.layer addSublayer:layer];

 

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx

{

    CGContextSetRGBFillColor(ctx, 1.0, 0, 0, 1.0);

    CGContextAddRect(ctx, CGRectMake(0, 0, 20, 20));

    CGContextFillPath(ctx);

} 

 

    CABasicAnimation *basicAnimation = [CABasicAnimation animation];

    basicAnimation.keyPath = @"position";

    basicAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, 0)];

    basicAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 200)];

    basicAnimation.duration = 1.0;

    

    basicAnimation.removedOnCompletion = NO;//动画执行完以后不要删除动画

    basicAnimation.fillMode = kCAFillModeForwards;//保持最新的状态

    

    [self.layer addAnimation:basicAnimation forKey:nil]; 

posted @ 2015-04-23 18:31  罕见  阅读(239)  评论(0编辑  收藏  举报