gpt4 book ai didi

iphone - iOS:绘图线出现在 subview 后面

转载 作者:可可西里 更新时间:2023-11-01 05:06:59 28 4
gpt4 key购买 nike

我有一个简单的 UIView,在 drawRect 中:我正在添加一个 UIImageView 作为 subview ,然后尝试在该 subview 的顶部画一条线。但是,这条线在 imageview 后面绘制。

如何使绘图上下文成为 subview 以便我可以在它上面绘图?

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 10.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextBeginPath(context);
CGContextMoveToPoint(context, 0, 0);
CGContextAddCurveToPoint(context,125,150,175,150,200,100);
CGContextAddCurveToPoint(context,225,50,275,75,300,200);
CGContextStrokePath(context);

最佳答案

创建另一个自定义 View ,它的唯一工作是画线。将其添加为与 ImageView 具有相同框架的 subview ,并调用 bringSubviewToFront 以确保它在前面。您可能必须在自定义 View 上设置一些属性,例如 opaque = NO并将背景颜色设置为清除 ( [UIColor colorWithWhite:0.0 alpha:0.0] )。

顺便说一句,不要在 drawRect 中添加任何 subview 。 drawRect 应该只是绘制,而不是更改 View 属性或层次结构。您应该在其他地方添加 ImageView 和自定义线条绘图 View ,也许在 init.像这样:

@implementation MyView

-(id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
imageView = ... // however you create your image view
[self addSubview:imageView];
lineView = [[MyLineView alloc] initWithFrame:imageView.frame];
[self addSubview:lineView];
[self bringSubviewToFront:lineView];
}
return self;
}

...

@end

@implementation MyLineView

-(void)drawRect:(CGRect)rect {
// your drawing code
// remember the coordinate system now has (0, 0) at the top left corner of the
// image view, so adjust your drawing code accordingly
}

@end

关于iphone - iOS:绘图线出现在 subview 后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7167757/

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