2013-08-29 32 views
7

Tôi cố gắng để thay đổi bán kính góc của một nút (OpenNoteVisible.layer) theo cách sau:Thay đổi cornerRadius sử dụng 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"]; 

Nhưng mã này đưa ra một lỗi ở dòng [animation.layer setCornerRadius : 140.0]; Tôi không thể hiểu tại sao. Tôi đã nhập khung lõi thạch anh.

+0

Bạn có thể thiết lập bán kính góc để nút. Vì vậy, sử dụng các đối tượng nút – SRI

Trả lời

20

Bạn đang đặt bán kính góc trên thuộc tính lớp của đối tượng hoạt ảnh; đối tượng hoạt ảnh này không có thuộc tính lớp.

Bạn cần đặt bán kính góc trên lớp của thứ bạn đang tạo hoạt ảnh, trong trường hợp này là OpenNoteVisible. Bạn cũng cần phải đảm bảo toValue đối tượng hoạt ảnh khớp với giá trị bạn đang đặt trên lớp, nếu không bạn sẽ nhận được hình động lẻ.

Mã của bạn bây giờ sẽ là:

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

Swift giải pháp 4 dưới

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 
    }) 
}) 
Các vấn đề liên quan