2013-08-29 43 views
7

我试图改变一个按钮(OpenNoteVisible.layer)的下列方式拐角半径:使用核心动画更改cornerRadius

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核心框架。

+0

您可以设置角半径按钮。所以使用按钮对象 – SRI

回答

20

您正在设置动画对象的图层属性的拐角半径;此动画对象不具有图层属性。

您需要在动画的图层上设置角落半径,在这种情况下为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"]; 
0

斯威夫特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 
    }) 
})