問題:インターフェイスビルダーの代わりに、実行時にビュー上のクラスを設定する方法
私はビュー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