2016-06-24 38 views
0

目前我在UIView中有一個Method,它已被添加爲子視圖。我想知道如何保留Method的屬性,使其框架規定UIView的大小。將UIView框架綁定到另一個類中的方法框架

NSObject的:

public class func drawCanvas1(frame frame: CGRect = CGRect(x: 0, y: 0, width: 86, height: 31)) { 

    let rectanglePath = UIBezierPath(rect: CGRect(x: frame.minX + frame.width - 86, y: frame.minY + floor((frame.height) * 0.00000 - 0.5) + 1, width: 86, height: frame.height - 1 - floor((frame.height) * 0.00000 - 0.5))) 
    UIColor.grayColor().setFill() 
    rectanglePath.fill() 

} 

的UIView:

class shapeTestUI: UIView { 

    override func drawRect(rect: CGRect) {   
     StyleKitName.drawCanvas1()   
    } 
} 

視圖控制器:

var block1: shapeTestUI = shapeTestUI() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.view.backgroundColor = UIColor(red: 38.0/255, green: 151.0/255, blue: 68.0/255, alpha: 1) 

    let block = createBlock(block1) 
    self.view.addSubview(block) 

} 

func createBlock(blocks:UIView) -> UIView { 

    let block = blocks as UIView! 
    //block.frame = CGRectMake(0, 0, 50, 50) 

    return block 
} 

回答

1

首先,您的代碼不會顯示作爲子視圖添加的「方法」,因爲這是不可能的。閱讀:https://en.wikipedia.org/wiki/Method_(computer_programming)

其次,重寫drawRect函數不添加一個子視圖而是根據它裏面的代碼(即,PaintCode StyleKit功能)繪製UIView本身。

第三,如果你想了UIView的框架由繪圖代碼來決定改變像這樣的PaintCode功能:

public class func drawCanvas1(frame: CGRect) { 
    let rectanglePath = UIBezierPath(rect: CGRect(x: frame.minX + frame.width - 86, y: frame.minY + floor((frame.height) * 0.00000 - 0.5) + 1, width: 86, height: frame.height - 1 - floor((frame.height) * 0.00000 - 0.5))) 
    UIColor.grayColor().setFill() 
    rectanglePath.fill() 
} 

接下來,在shapesTestUI,你應該通過rectdrawRect參數修改後PaintCode功能:

class shapeTestUI: UIView { 

    override func drawRect(rect: CGRect) {   
     StyleKitName.drawCanvas1(rect)   
    } 
} 

而在去年,你應該在你的瀏覽精讀初始化block1時得到期望的CGRect troller

var block1: shapeTestUI = shapeTestUI(frame: CGRect(x: 0, y: 0, width: 86, height: 31)) 
+0

謝謝,艾克。本週您幫助我的第二或第三個問題。 Apreciate它。 – Geppelt

相關問題