【问题标题】:drawRect: being called, only shows the frame not the drawingdrawRect:被调用,只显示框架而不是绘图
【发布时间】:2013-09-23 19:29:03
【问题描述】:

我有一个 UIControl 子类,我在其中实现了 drawRect。我正在尝试使用UIBezierPathdrawRect 方法内绘制一个矩形。所有适当的方法都被调用,没有什么是空的,应用程序运行没有问题。问题是,只画了框架。意思是,我可以看到框架的区域,因为它是黑色的,但我看不到画在框架上的任何东西。

这是我为实例化类所做的工作:

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 没有被绘制。另外,不确定使用选择器是否是最好的方法。有人知道这是怎么回事吗?

【问题讨论】:

    标签: objective-c cocoa drawrect uibezierpath


    【解决方案1】:

    您似乎没有调用drawButtonWithLeftCornersRounded 方法。我认为这条线是错误的:

    [self performSelector:self.buttonType withObject:nil];
    

    但应该是:

    [self performSelector:@selector(drawButtonWithLeftCornersRounded) withObject:nil];
    

    希望这会有所帮助!

    【讨论】:

    • drawButtonWithLeftCornersRounded 被调用。 self.buttonType 是一个选择器。我尝试了您的更改,结果相同。
    【解决方案2】:

    解决了。在drawButtonWithLeftCornersRounded bezierPathWithRoundedRect 中不应为self.frame。这将导致 x 和 y 坐标位于框架内的 x、y 位置,而不是视图。我把它的参数改成这样:

    CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-10
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多