我有一个UIControl
子类,我已经实现了drawRect
。 我正在尝试使用UIBezierPath
在drawRect
方法内部绘制一个矩形。 正在调用所有适当的方法,没有任何内容为空,并且应用程序运行没有问题。 问题是,除了画框架之外什么都没有。 意思是,我可以看到框架的区域,因为它是黑色的,但我看不到任何画在框架上的东西。
以下是我正在做的实例化类:
ZHButton *button =
[[ZHButton alloc] initWithFrame:CGRectMake(100, 200, 100, 50)];
button.buttonType = NSSelectorFromString(LEFT_ROUNDED_CORNER_BUTTON);
[self.view addSubview:button];
下面是 drawRect: 方法,它使用选择器调用不同的方法:
- (void)drawRect:(CGRect)rect {
UIBezierPath *button = [self performSelector:self.buttonType withObject:nil];
[[UIColor orangeColor] setFill];
[button fill];
}
- (UIBezierPath *)drawButtonWithLeftCornersRounded {
return [UIBezierPath bezierPathWithRoundedRect:self.frame
byRoundingCorners:UIRectCornerTopLeft |
UIRectCornerBottomLeft
cornerRadii:buttonRadii];
}
一切似乎都正常,但UIBezierPath
没有被绘制出来。 另外,不确定使用选择器是否是执行此操作的最佳方法。 有谁知道这是怎么回事?
您似乎没有调用drawButtonWithLeftCornersRounded
方法。我认为这句话是错误的:
[self performSelector:self.buttonType withObject:nil];
但它应该是:
[self performSelector:@selector(drawButtonWithLeftCornersRounded) withObject:nil];
希望这有帮助!
解决了。 在drawButtonWithLeftCornersRounded
bezierPathWithRoundedRect
不应该self.frame
. 这将导致 x 和 y 坐标位于框架内的 x, y 位置,而不是视图。 我将其参数更改为:
CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);