SSubViewのSubViewの大きさを動的に変えたいのですが上手くいきません。
具体的にはサムネイルをaddSubViewしたUIButtonのサブクラスをViewCOntrollerにaddSubViewし、そのボタンのタップイベントをフックにサムネイルの大きさを変更したいです。
色々試したのですが、大きさを変えることができず、どなたか詳しい方がいらっしゃいましたらご教授頂きたいです。
よろしくお願いします。
import UIKit class CustomButton: UIButton { private var width:CGFloat = 0 private var height:CGFloat = 0 private var margin:CGFloat = 8 private var offset:CGFloat = 8 var thumbnail:UIImageView! var label:UILabel! required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } override init(frame: CGRect) { super.init(frame: frame) layer.masksToBounds = false layer.cornerRadius = 4 layer.shadowOffset = CGSize(width: 0, height: 2) layer.shadowRadius = 4; layer.shadowOpacity = 0.4; backgroundColor = UIColor.init(named: "unTap") thumbnail = UIImageView() thumbnail.image = UIImage(named: "icon") self.addSubview(thumbnail) label = UILabel() label.text = "Hello" label.font = UIFont.systemFont(ofSize: 40) self.addSubview(label) } override func layoutSubviews() { width = frame.width height = frame.height margin = 8 offset = 8 unTapPosition() } func unTapPosition() { thumbnail.frame = CGRect(x: margin, y: margin, width: height - margin * 2, height: height - margin * 2) label.frame = CGRect(x: height, y: 0, width: width - height, height: height) } func onTapPosition() { thumbnail.frame = CGRect(x: margin + offset, y: margin, width: height - (margin * 2 + offset), height: height - margin * 2) label.frame = CGRect(x: height, y: 0, width: width - height, height: height) } func unTap() { backgroundColor = UIColor.init(named: "unTap") unTapPosition() } func onTap() { backgroundColor = UIColor.init(named: "onTap") onTapPosition() } }
ViewController.Swift
1import UIKit 2 3class ViewController: UIViewController { 4 let button = CustomButton() 5 6 override func viewDidLoad() { 7 super.viewDidLoad() 8 9 view.backgroundColor = .white 10 11 let width = view.frame.width 12 let height = view.frame.height 13 let buttonWidth = width * 0.8 14 let buttonHeight: CGFloat = 80 15 16 // UIButtonのインスタンスを作成する 17 button.addTarget(self, action: #selector(touchUpInside(_:)), for: UIControl.Event.touchUpInside) 18 button.addTarget(self, action: #selector(touchDown(_:)), for: UIControl.Event.touchDown) 19 button.addTarget(self, action: #selector(touchDragExit(_:)), for: UIControl.Event.touchDragExit) 20 button.setTitle("ボタンのテキスト", for: UIControl.State.normal) 21 button.setTitleColor(.red, for: UIControl.State.normal) 22 button.frame = CGRect(x: (width - buttonWidth) / 2, y: (height - buttonHeight) / 2, width: buttonWidth, height: buttonHeight) 23 view.addSubview(button) 24 } 25 26 27 @objc func touchDown(_ sender: UIButton) { 28 print("touchDown") 29 (sender as! CustomButton).onTap() // 大きさを変えたい 30 } 31 32 @objc func touchUpInside(_ sender: UIButton) { 33 print("touchUpInside") 34 (sender as! CustomButton).unTap() 35 } 36 37 @objc func touchDragExit(_ sender: UIButton) { 38 print("touchDragExit") 39 (sender as! CustomButton).unTap() 40 } 41} 42
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。