无法在UIView内部绘制UIImage

时间:2016-05-14 08:00:49

标签: ios objective-c uiview uiimage

我有一个自定义的UIView,在其中我必须将图像显示为背景并使用触摸和拖动自由绘制,引用此问题iOS:iOS: Drawing line is appearing behind subviews, 但似乎没有工作,它绘制图像,但没有任何东西,请帮助

以下自定义UiView自由选择

- (id)initWithCoder:(NSCoder *)aDecoder // (1)
{
    if (self = [super initWithCoder:aDecoder])
    {
        [self setMultipleTouchEnabled:NO]; // (2)
        [self setBackgroundColor:[UIColor whiteColor]];
        path = [UIBezierPath bezierPath];
        [path setLineWidth:2.0];
    }


    return self;
}

- (void)drawRect:(CGRect)rect // (5)
{

    [[UIColor blackColor] setStroke];
    [path stroke];

}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    [path moveToPoint:p];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    [path addLineToPoint:p]; // (4)
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesMoved:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesEnded:touches withEvent:event];
}


@end

这是我添加UiImage和OverlayView

的主要UIView
- (id)initWithCoder:(NSCoder *)aDecoder // (1)
{
    if (self = [super initWithCoder:aDecoder])
    {
        CGRect rect1 = self.frame;
        rect1.origin.x = 0;
        rect1.origin.y = 0;

        UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(rect1.origin.x,rect1.origin.y,rect1.size.width,rect1.size.height)];
        dot.image = [UIImage imageNamed:@"miter"];
        [self addSubview:dot];

        OverlayView *overlay = [[OverlayView alloc] initWithFrame:CGRectMake(rect1.origin.x,rect1.origin.y,rect1.size.width,rect1.size.height)];
        [overlay setBackgroundColor:[UIColor clearColor]];

        [self addSubview:overlay];
        [self bringSubviewToFront:overlay];

    }


    return self;
}



@end

1 个答案:

答案 0 :(得分:0)

将userInteractionEnabled属性设置为YES。 UIViews的默认值为YES,但对于UIImageViews为NO,因此您必须明确打开它。