objective-c - UIImage 调整绘制矩形大小并用黑色填充背景

标签 objective-c ios core-graphics drawrect

我有一个名为 MNTRectangle 的类,它是 UIImage 的子类。我已经重写了此类的drawRect方法来在图像上绘制边框(使用其框架)。我拥有它,因此当用户在名为 DocumentView(UIView 的子类)的类上启动平移/拖动手势时,它会将 MNTRectangle 的实例作为 subview 添加到 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)];
    }
}

任何有关此问题的帮助将不胜感激。

最佳答案

我终于明白了。在我设置矩形框架后的 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];
    }
}

关于objective-c - UIImage 调整绘制矩形大小并用黑色填充背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13772073/

相关文章:

ios - XCode IBAction 和 IBOutlets 错误

ios - 自定义 UITableViewCell 的布局何时完成?

iOS:核心数据错误:NSManagedObject 的 NSMergeConflict

objective-c - 拥有许多小的 CATextLayer 会导致调整窗口大小的速度变慢

objective-c - 有没有办法使用内存映射文件访问从文件创建 CFData 对象?

ios - 如果 UIGestureRecognizer 触发,如何取消按钮点击?

ios - 如何收到用户使用 ShareKit 分享内容的通知?

cocoa - CGImageGetWidth 返回与实际图像不同的尺寸

c++ - 在 Carbon 中创建 JPEG 时的 JPEG 质量

swift - 从灰度矩阵创建 CGImage/UIImage