2016-08-21 6 views
0

カスタムUIViewを作成する方法を学習中です。この特定のビューにはカップルのボタンが含まれています。私は、遅延のインスタンス化ブロック内からframe/heightプロパティを呼び出すときに値128を取得したことに気付きましたが、drawRect関数で渡されたrect引数のheightプロパティを境界と一緒に呼び出すと、値94が取得されます。UIView境界/フレームがdrawRectと一致しません

のdrawRect関数から呼び出された境界/フレーム幅/高さのプロパティが初期化に使用されるフレーム/境界の高さと幅と整列していない理由を私は理解していません。誰かがこれを説明できますか?

@IBDesignable 
class GroupingMetaDataView: UIView { 

    @IBInspectable var strokeColor: UIColor = 
     UIColor.blackColor().colorWithAlphaComponent(0.4) 

    @IBInspectable var strokeWidth: CGFloat = 2.0 


    lazy var subButton: AddButton! = { 
     print("frame height: \(self.frame.height)") 
     print("bounds height: \(self.bounds.height)") 
     let width = self.frame.width * 0.087 
     let size = CGSize(width: width, height: width) 
     let frame = CGRect(origin: CGPoint(x: self.frame.width * 0.055, 
              y: self.frame.height * 0.5), size: size) 
     let button = AddButton(frame: frame, isAddButton: false) 

     return button 
    }() 

    lazy var addButton: AddButton! = { 
     let width = self.frame.width * 0.087 
     let size = CGSize(width: width, height: width) 
     let frame = CGRect(origin: CGPoint(x: self.frame.width * 0.857, 
              y: self.frame.height), size: size) 
     let button = AddButton(frame: frame, isAddButton: true) 

     return button 
    }() 

    override init(frame: CGRect) { 
     super.init(frame: frame) 

     self.setupUI() 

    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     self.setupUI() 
    } 

    // Only override drawRect: if you perform custom drawing. 
    // An empty implementation adversely affects performance during animation. 
    override func drawRect(rect: CGRect) { 
     // Drawing code 
     print("rect height: \(rect.height)") 
     print("boundsheight: \(bounds.height)") 

     let context = UIGraphicsGetCurrentContext() 
     CGContextSetLineWidth(context, strokeWidth) 
     strokeColor.setStroke() 

     let topBarPath = UIBezierPath() 
     topBarPath.moveToPoint(CGPoint(x: 0.0, y: 2.0)) 
     topBarPath.addLineToPoint(CGPoint(x: rect.width, y: 2.0)) 

     topBarPath.lineWidth = strokeWidth 
     topBarPath.stroke() 

     let bottomBarPath = UIBezierPath() 
     bottomBarPath.moveToPoint(CGPoint(x: 0.0, y: rect.height-2.0)) 
     bottomBarPath.addLineToPoint(CGPoint(x: rect.width, y: rect.height-2.0)) 

     bottomBarPath.lineWidth = strokeWidth 
     bottomBarPath.stroke() 
    } 

    // MARK: Setup 

    func setupUI() { 
     self.backgroundColor = UIColor.yellowColor() 
     addSubview(subButton) 
     addSubview(addButton) 
    } 
} 

編集:

私はViewControllerをからGroupingMetaDataViewを作成します。 YardageMetaDataViewはGroupingMetaDataViewのサブクラスです。 YardageMetaDataViewはカスタム図面を実行しません。

私はのdrawRectパラメータのドキュメントに気づいを更新する必要がありますビューの境界の

部分と述べています。最初に ビューが描画されるとき、この矩形は通常、ビュー全体の表示範囲である です。ただし、その後の描画中には、表示の一部のみを指定することがあります。

なぜ、パラメータrectはビューの一部だけを指定するのですか? drawRectは、setNeedsDisplayがトリガーされた後、次の描画サイクルで呼び出されます。 SetNeedsDisplayはパラメータを取らないので、パラメータがビュー全体を表すと仮定していますか?

ViewController.swift setNeedsDisplay(_ :)呼ばsetNeedsDisplayに類似した別の方法があり

class ViewController: UIViewController { 

// MARK: - Properties 

    lazy var yMetaDataView: YardageMetaDataView! = { 
     let view = YardageMetaDataView(frame: CGRect(origin: CGPoint(x: 50, y: 100), 
            size: CGSize(width: self.view.bounds.width * 0.75, 
               height: self.view.bounds.height * 0.102))) 
     view.translatesAutoresizingMaskIntoConstraints = false 
     return view 
    }() 
} 
+0

親ビューに追加するコードはどこですか? – nhgrif

+0

@nhgrifビューをインスタンス化するコードを追加したところです。 – gofly

答えて

0

。後者が呼び出されると、へのdrawRect(_ :)を渡されたパラメータを使用すると、その呼び出しに渡されたものです。その矩形領域は無効とマークされ、再描画する必要があります。

あなたが電話したかどうかわからないsetNeedsDisplay(_ :)。それはあなたの仕事です。がんばろう。 :)

関連する問題