将 UIView 框架绑定到另一个 class 内的方法框架
Bound UIView frame to Method frame inside another class
目前我在 UIView
中有一个 Method
已添加为子视图。我想知道如何保留方法的属性,以便其框架决定 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
}
首先, 您的代码没有显示 "method" 被添加为子视图,因为这是不可能的。读这个:https://en.wikipedia.org/wiki/Method_(computer_programming).
其次,重写drawRect
函数并不会添加子视图,而是根据其中的代码(即PaintCode)绘制UIView
本身StyleKit 函数)。
第三, 如果您希望 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
中,将drawRect
的参数rect
传递给修改后的PaintCode函数:
class shapeTestUI: UIView {
override func drawRect(rect: CGRect) {
StyleKitName.drawCanvas1(rect)
}
}
最后,在你的 View Controllerblock1
中初始化 block1
时,你应该提供所需的 CGRect
var block1: shapeTestUI = shapeTestUI(frame: CGRect(x: 0, y: 0, width: 86, height: 31))
目前我在 UIView
中有一个 Method
已添加为子视图。我想知道如何保留方法的属性,以便其框架决定 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
}
首先, 您的代码没有显示 "method" 被添加为子视图,因为这是不可能的。读这个:https://en.wikipedia.org/wiki/Method_(computer_programming).
其次,重写drawRect
函数并不会添加子视图,而是根据其中的代码(即PaintCode)绘制UIView
本身StyleKit 函数)。
第三, 如果您希望 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
中,将drawRect
的参数rect
传递给修改后的PaintCode函数:
class shapeTestUI: UIView {
override func drawRect(rect: CGRect) {
StyleKitName.drawCanvas1(rect)
}
}
最后,在你的 View Controllerblock1
中初始化 block1
时,你应该提供所需的 CGRect
var block1: shapeTestUI = shapeTestUI(frame: CGRect(x: 0, y: 0, width: 86, height: 31))