7

我正在尝试通过以下方式更改按钮(OpenNoteVisible.layer)的角半径:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
animation.timingFunction = [CAMediaTimingFunction     functionWithName:kCAMediaTimingFunctionLinear];
animation.fromValue = [NSNumber numberWithFloat:10.0f];
animation.toValue = [NSNumber numberWithFloat:0.0f];
animation.duration = 1.0;
[animation.layer setCornerRadius:140.0];
[OpenNoteVisible.layer addAnimation:animation forKey:@"cornerRadius"];

但是这段代码在 [animation.layer setCornerRadius:140.0]; 行给出了错误。我不明白为什么。我已经导入了 Quartz 核心框架。

4

2 回答 2

23

您正在动画对象的 layer 属性上设置圆角半径;此动画对象没有图层属性。

在这种情况下,您需要在动画对象的图层上设置圆角半径OpenNoteVisible。您还需要确保toValue动画对象的值与您在图层上设置的值相匹配,否则您会得到奇怪的动画。

您的代码现在应该是:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
animation.timingFunction = [CAMediaTimingFunction     functionWithName:kCAMediaTimingFunctionLinear];
animation.fromValue = [NSNumber numberWithFloat:10.0f];
animation.toValue = [NSNumber numberWithFloat:140.0f];
animation.duration = 1.0;
[OpenNoteVisible.layer setCornerRadius:140.0];
[OpenNoteVisible.layer addAnimation:animation forKey:@"cornerRadius"];
于 2013-08-29T12:49:10.980 回答
0

Swift 4解决方案如下

import UIKit
import PlaygroundSupport

let view = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
view.backgroundColor = #colorLiteral(red: 1, green: 0.5763723254, blue: 0, alpha: 1)
PlaygroundPage.current.liveView = view

UIView.animate(withDuration: 2.5, animations: {
    view.layer.cornerRadius = 40
}, completion: { _ in
    UIView.animate(withDuration: 0.5, animations: {
        view.layer.cornerRadius = 0
    })
})
于 2018-01-27T16:33:29.037 回答