문제 :인터페이스 빌더 대신 런타임시 뷰에 클래스를 설정하는 방법
내가보기 myView
에 부착 된 아래의 클래스 myShadow
있습니다.
클래스는 인터페이스 작성기에서 사용자 정의 클래스로보기에 첨부됩니다.
그러나 앱이 실행되는 동안 클래스를 설정해야 할 때가 있습니다.
질문 : 내가 대신 프로그래밍 방식으로 사용자가 버튼 myButton
도청 예를 들어보기 실행시 myView
에 클래스 myShadow
을 설정하는 방법
? 클래스를 정규 함수로 호출하는 방법이 있나요?
ViewController
?
코드 :
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myView: UIView!
@IBAction func myButton(sender: AnyObject) {
//// How to set `myShadow` class on `myView` here when tapping `myButton`? ////
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
class myShadow: UIView {
var shadowLayer: CAShapeLayer!
override func layoutSubviews() {
super.layoutSubviews()
if shadowLayer == nil {
shadowLayer = CAShapeLayer()
shadowLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: 5).CGPath
shadowLayer.fillColor = UIColor.yellowColor().CGColor
shadowLayer.shadowColor = UIColor.blackColor().CGColor
shadowLayer.shadowPath = shadowLayer.path
shadowLayer.shadowOffset = CGSize(width: 0, height: 0)
shadowLayer.shadowOpacity = 1
shadowLayer.shadowRadius = 5
layer.insertSublayer(shadowLayer, atIndex: 0)
}
}
}
감사합니다. 이것들은 좋은 옵션입니다! – user4806509