2013-08-29 1 views
7

を使用して:cornerRadiusを変更し、私は次のようにボタン(OpenNoteVisible.layer)の角の半径を変更しようとしていますCore Animationの

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 
    }) 
}) 
関連する問題