【问题标题】:Cant drawing rectangles over image in custom UIView无法在自定义 UIView 中的图像上绘制矩形
【发布时间】:2013-08-10 14:06:53
【问题描述】:

我有一个带有 UIVIewcontroller 的情节提要场景。在这个场景中,我有一个 UIImageview,其中包含背景图像、一个 UIButton 和一个 UIView。

这个 UIView 有一个覆盖的 drawRect 方法:

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    [self setNeedsDisplay];


    CGFloat height = self.bounds.size.height;
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextClearRect(context, rect);
    CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
    CGFloat barWidth = 30;
    int count = 0;
    NSArray *values = [NSArray arrayWithObjects:@1, @0.5, nil];
    for (NSNumber *num in values) {
        CGFloat x = count * (barWidth + 10);
        CGRect barRect = CGRectMake(x, height - ([num floatValue] * height), barWidth, [num floatValue] * height);
        CGContextAddRect(context, barRect);
        count++;
    }
    CGContextFillPath(context);

}

我的问题是:如何将图像设置为自定义 UIView 的背景并在其上绘制矩形?

问候

【问题讨论】:

    标签: ios uiview uiviewcontroller drawrect


    【解决方案1】:

    假设您将 UIView 子类命名为 MyCustomView。从 interface-builder(xib 或 storyboard)添加 UIView 时,您必须明确地将 interface builder 中的 UIView 的自定义类设置为 MyCustomView(就像在这个 answer 中一样)。

    另一个可能出现的问题是视图的顺序。哪一个在上面?

    从代码中添加自定义视图是另一种方法。

    您的绘图代码似乎没问题。这是我来自drawRect 的代码,它在背景中绘制图像(我稍微调整了一下):

    - (void)drawRect:(CGRect)rect
    {
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextClearRect(context, rect);
    
        // here I draw an image BEFORE drawing the rectangles
        UIImage* img = [UIImage imageNamed:@"img.jpg"];
        [img drawInRect:rect];
    
        CGFloat height = self.bounds.size.height;
        CGFloat barWidth = 30;
    
        CGContextSetFillColorWithColor(context, [[UIColor grayColor] CGColor]);
    
        int count = 0;
        NSArray *values = [NSArray arrayWithObjects:@1, @0.5, nil];
        for (NSNumber *num in values) {
            CGFloat x = count * (barWidth + 10);
            CGRect barRect = CGRectMake(x, height - ([num floatValue] * height), barWidth, [num floatValue] * height);
    
            // drawing rectangles OVER the image
            CGContextFillRect(context, barRect);
            count++;
        }
    }
    

    【讨论】:

      【解决方案2】:

      这是为您自定义的UIView 设置背景图片(放在drawRect: 方法中):

       self.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"someimage.png"]];
      

      如果您想将子视图添加到自定义 UIView,您可以直接在情节提要上执行此操作,或者以编程方式使用 addSubview 方法:

      UIView *v = [[UIView alloc]initWithFrame:CGRectMake(x,y,w,h)];//change x,y,w,h to meet yours
      [self.myCustomView addSubview:v];
      

      当然,addSubview 会处理所有的UIView 子类,所以你可以添加UIImageViewUIScrollView 等。

      【讨论】:

      • 如果我添加背景颜色不执行任何操作..如果我添加子视图,然后将 UIImageview 添加到子视图,此子视图会覆盖图表的栏,我无法查看图表..
      猜你喜欢
      • 2017-08-04
      • 2018-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多