CAShapeLayer动画iOS7的问题

时间:2015-11-11 10:48:40

标签: ios objective-c animation calayer cashapelayer

我一直在努力研究iOS7上的动画效果(8,9工作正常),包括将圆角矩形缩小成圆圈。为了得到这样的效果。

Desired Effect

但是当我试图在iOS7上收缩圈时,我得到了一些失真。

Distorted Effect on iOS7

我的视图控制器很简单

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.fillColor = [UIColor blackColor].CGColor;
    layer.frame = CGRectMake(100, 100, 100, 100);
    layer.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 100, 50) cornerRadius:25].CGPath;
    self.myLayer = layer;
    [self.view.layer addSublayer:layer];

}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"path"];
    animation.values = @[
                         (id)[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 100, 50) cornerRadius:25].CGPath,
                         (id)[UIBezierPath bezierPathWithRoundedRect:CGRectMake(5, 0, 90, 50) cornerRadius:25].CGPath,
                         (id)[UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 0, 80, 50) cornerRadius:25].CGPath,
                         (id)[UIBezierPath bezierPathWithRoundedRect:CGRectMake(15, 0, 70, 50) cornerRadius:25].CGPath,
                         (id)[UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 0, 60, 50) cornerRadius:25].CGPath,
                         (id)[UIBezierPath bezierPathWithRoundedRect:CGRectMake(25, 0, 50, 50) cornerRadius:25].CGPath,
                         ];

    animation.keyTimes = @[@(0),
                           @(0.18),
                           @(0.36),
                           @(0.54),
                           @(0.72),
                           @(1),
                           ];

    animation.duration = 10;
    animation.fillMode = kCAFillModeForwards;
    [self.myLayer addAnimation:animation forKey:nil];
    self.myLayer.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(25, 0, 50, 50) cornerRadius:25].CGPath;
}

1 个答案:

答案 0 :(得分:2)

这似乎是iOS 7的一个错误。看看这个链接:

http://www.paintcodeapp.com/blogpost/code-for-ios-7-rounded-rectangles

帖子底部有一个类别方法可以解决问题。 我用你的代码测试了它,它似乎工作!我不得不做一个小改动,即取代

CGFloat limitedRadius = MIN(radius, limit);

CGFloat limitedRadius = radius;

这允许角半径变得足够大以创建圆。可能值得研究为什么角落半径首先受到限制,但我会留给你:)