gpt4 book ai didi

objective-c - UIImage 调整大小 drawRect 用黑色填充背景

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:17:25 25 4
gpt4 key购买 nike

我有一个名为 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 调整大小 drawRect 用黑色填充背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13772073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com