gpt4 book ai didi

iphone - 带有选项的 UIView drawRect 方法

转载 作者:行者123 更新时间:2023-11-29 04:31:01 26 4
gpt4 key购买 nike

我想创建一个UIView Class您可以使用多个选项进行初始化。

我认为问题出在 drawRect() 方法上。是否可以使用特定选项初始化类,例如

[[MyUIView alloc]initWithColor:[UIColor blueColor]]

我读到,在没有初始化对象的情况下,永远不应该调用 drawRect() 方法。

那么如何在不手动调用的情况下实现上面的语句呢?

到目前为止我已经尝试过:

我认为我可以初始化 View 然后调用方法

[MyView changeColorTo:green]

它尝试重绘 View ,但我无法让它工作。

也许我在 drawRect() 方法中使用 coreGraphics(具有某种颜色,应该是可选的)绘制圆角矩形很重要。

我如何实现我想要的行为?

最佳答案

通话

[self setNeedsDisplayInRect:aCGRect];

为你调用drawRect:。

编辑颜色属性:

@interface MyView: UIView

@property (nonatomic, strong) UIColor *drawColor;

@end

@implementation MyView

@synthesize drawColor;

- (id)initWithWhatever
{
self.drawColor = [UIColor infraRedColor];
[self setNeedsDisplayInRect:aRect];
}

- (void)drawRect:(CGRect)r
{
// use self.drawColor
}

@end

关于iphone - 带有选项的 UIView drawRect 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11729677/

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