2016-06-24 3 views
0

현재 서브 뷰로 추가 된 UIView 안에 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 기능에 : 당신의보기 콘에 block1을 초기화 할 때

class shapeTestUI: UIView { 

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

그리고 마지막으로, 당신은 원하는 CGRect를 제공해야 유모차

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

감사합니다. 두 번째 또는 세 번째 질문은 이번 주에 저를 도왔습니다. 그것을 비난하십시오. – Geppelt