Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

清单8.4 使用KVC对动画打标签 代码有问题 #20

Open
panzhou opened this issue Feb 10, 2017 · 0 comments
Open

清单8.4 使用KVC对动画打标签 代码有问题 #20

panzhou opened this issue Feb 10, 2017 · 0 comments

Comments

@panzhou
Copy link

panzhou commented Feb 10, 2017

我仔细看了下代码,按照那里的写法的确能够成功执行。但是我发现

- (void)setAngle:(CGFloat)angle forHand:(UIView *)handView animated:(BOOL)animated
{
    //generate transform
    CATransform3D transform = CATransform3DMakeRotation(angle, 0, 0, 1);
    if (animated) {
        //create transform animation
        CABasicAnimation *animation = [CABasicAnimation animation];
        [self updateHandsAnimated:NO];
        animation.keyPath = @"transform";
        animation.toValue = [NSValue valueWithCATransform3D:transform];
        animation.duration = 0.5;
        animation.delegate = self;
        [animation setValue:handView forKey:@"handView"];
        [handView.layer addAnimation:animation forKey:nil];
    } else {
        //set transform directly
        handView.layer.transform = transform;
    }
}

这段代码里面 修改成

- (void)setAngle:(CGFloat)angle forHand:(UIView *)handView animated:(BOOL)animated
{
    //generate transform
    CATransform3D transform = CATransform3DMakeRotation(angle, 0, 0, 1);
    handView.layer.transform = transform;
}

一样能成功执行,因为以上代码主要的作用只是做了一个transform转换,并没有达到CABasicAnimation的目的。我觉得其实作者想要实现的效果应该是

- (void)setAngle:(CGFloat )angle forHand:(UIImageView *)handView animated:(BOOL)animated
{
    CATransform3D transform = CATransform3DMakeRotation(angle, 0, 0, 1);
    if (animated) {
        CABasicAnimation *animation = [CABasicAnimation animation];
        animation.keyPath = @"transform";
        animation.toValue = [NSValue valueWithCATransform3D:transform];
        animation.duration = 0.5;
        animation.fillMode = kCAFillModeForwards;
        animation.removedOnCompletion = NO;
        [handView.layer addAnimation:animation forKey:nil];
    }
    else
    {
        handView.layer.transform = transform;
    }
}

作者可以参考下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant