UIImage用黑色调整drawRect填充背景

时间:2012-12-07 22:32:00

标签: objective-c ios core-graphics drawrect

我有一个名为MNTRectangle的类,它是UIImage的子类。我已经覆盖了这个类的drawRect方法,在图像上绘制一个边框(使用它的框架)。我知道当用户在名为DocumentView(UIView的子类)的类上启动平移/拖动手势时,它会将MNTRectangle的实例添加为DocumentView实例的子视图。当用户继续拖动时,MNTRectangle被调整大小。问题是MNTRectangle最终显示为纯黑色,我尝试清除图形上下文以及在绘制边框之前保存上下文并在绘制边框后恢复上下文。无论我怎样尝试,我都无法清除MNTRectangle,只是在调整大小时显示边框。

以下是我的MNTRectangle类的代码:

@implementation MNTRectangle

- (id)init
{
    self = [super init];

    if (self)
    {
        [self setup];
    }

    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];

    if (self)
    {
        [self setup];
    }

    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self)
    {
        [self setup];
    }

    return self;
}

- (void)setup
{
    [self setBackgroundColor:[UIColor clearColor]];
}

- (void)drawRect:(CGRect)rect
{
    // Get graphics context
    CGContextRef context = UIGraphicsGetCurrentContext();

//    CGContextSaveGState(context);

//    CGContextClearRect(context, rect);

    // Draw border
    CGContextSetLineWidth(context, 4.0);
    [[UIColor blackColor] setStroke];
    CGContextStrokeRect(context, rect);

//    CGContextRestoreGState(context);
}

以下是DocumentView中用于UIView上的平移/拖动处理的代码:

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if (self)
    {
         _panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    }

    return self;
}

- (void)handlePan:(UIPanGestureRecognizer *)sender
{
    if ([sender state] == UIGestureRecognizerStateBegan)
    {
        _startPanPoint = [sender locationInView:self];

        MNTRectangle *rectangle = [[MNTRectangle alloc] initWithFrame:CGRectMake(_startPanPoint.x, _startPanPoint.y, 1, 1)];
        [_objects addObject:rectangle];
        _currentPanObject = rectangle;
        [self addSubview:rectangle];
    }
    else if ([sender state] == UIGestureRecognizerStateChanged)
    {
        CGPoint endPanPoint = [sender locationInView:self];

        float height = fabsf(endPanPoint.y - _startPanPoint.y);
        float width = fabsf(endPanPoint.x - _startPanPoint.x);
        float x = MIN(_startPanPoint.x, endPanPoint.x);
        float y = MIN(_startPanPoint.y, endPanPoint.y);

        MNTRectangle *rectangle = (MNTRectangle *)_currentPanObject;
        [rectangle setFrame:CGRectMake(x, y, width, height)];
    }
    else if ([sender state] == UIGestureRecognizerStateEnded)
    {
        CGPoint endPanPoint = [sender locationInView:self];

        float height = fabsf(endPanPoint.y - _startPanPoint.y);
        float width = fabsf(endPanPoint.x - _startPanPoint.x);
        float x = MIN(_startPanPoint.x, endPanPoint.x);
        float y = MIN(_startPanPoint.y, endPanPoint.y);

        MNTRectangle *rectangle = (MNTRectangle *)_currentPanObject;
        [rectangle setFrame:CGRectMake(x, y, width, height)];
    }
}

对此的任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

我终于明白了。在我设置矩形框架后的handlePan:方法中,我遗漏了[rectangle setNeedsDisplay];

现在我的DocumentView代码如下所示:

-(id)initWithFrame:(CGRect)frame
{
   self = [super initWithFrame:frame];

    if (self)
    {
         _panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    }

    return self;
}

- (void)handlePan:(UIPanGestureRecognizer *)sender
{
   if ([sender state] == UIGestureRecognizerStateBegan)
   {
        _startPanPoint = [sender locationInView:self];

        MNTRectangle *rectangle = [[MNTRectangle alloc] initWithFrame:CGRectMake(_startPanPoint.x, _startPanPoint.y, 1, 1)];
        [_objects addObject:rectangle];
        _currentPanObject = rectangle;
        [self addSubview:rectangle];
    }
    else if ([sender state] == UIGestureRecognizerStateChanged)
    {
        CGPoint endPanPoint = [sender locationInView:self];

        float height = fabsf(endPanPoint.y - _startPanPoint.y);
        float width = fabsf(endPanPoint.x - _startPanPoint.x);
        float x = MIN(_startPanPoint.x, endPanPoint.x);
        float y = MIN(_startPanPoint.y, endPanPoint.y);

        MNTRectangle *rectangle = (MNTRectangle *)_currentPanObject;
        [rectangle setFrame:CGRectMake(x, y, width, height)];
        [rectangle setNeedsDisplay];
    }
    else if ([sender state] == UIGestureRecognizerStateEnded)
    {
        CGPoint endPanPoint = [sender locationInView:self];

        float height = fabsf(endPanPoint.y - _startPanPoint.y);
        float width = fabsf(endPanPoint.x - _startPanPoint.x);
        float x = MIN(_startPanPoint.x, endPanPoint.x);
        float y = MIN(_startPanPoint.y, endPanPoint.y);

        MNTRectangle *rectangle = (MNTRectangle *)_currentPanObject;
        [rectangle setFrame:CGRectMake(x, y, width, height)];
        [rectangle setNeedsDisplay];
    }
}

答案 1 :(得分:0)

你想打电话给[super drawRect:rect]

答案 2 :(得分:0)

为了澄清任何人阅读,需要调用[rectangle setNeedsDisplay];,因为你覆盖了drawRect:,因此需要手动告知UIViewMNTRectangle的这个子类在内部变量发生变化后重绘自己。

此函数在继承自UIView的标准对象上自动调用,但由于您已创建自定义对象,因此如果MNTRectangle中有某些特殊行为,则需要手动调用它这需要重绘。

进一步的文件here