iOS UIViewContentModeRedraw: 重新绘制视图内容
UIViewContentModeRedraw 是 UIView 的一个枚举值,用于控制视图的内容模式。当设置为 UIViewContentModeRedraw 时,视图会在需要重新绘制时调用 drawRect 方法重新绘制自己。
以下是一个使用 UIViewContentModeRedraw 的 OC 代码示例:
// 定义一个自定义的 UIView 子类
@interface MyView : UIView
@end
@implementation MyView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.contentMode = UIViewContentModeRedraw; // 设置内容模式为 Redraw
}
return self;
}
- (void)drawRect:(CGRect)rect {
// 在这里编写绘制代码
// ...
}
@end
// 在其他地方使用 MyView
// 创建一个 MyView 实例
MyView *myView = [[MyView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
// 添加到父视图中
[self.view addSubview:myView];
在上述代码中,我们创建了一个自定义的 UIView 子类 MyView,并在初始化方法中将其内容模式设置为 UIViewContentModeRedraw。然后,我们在 drawRect 方法中编写了自定义的绘制代码,当视图需要重新绘制时,系统会自动调用 drawRect 方法。
最后,我们在其他地方创建了一个 MyView 实例,并将其添加到父视图中,这样视图就会按照我们在 drawRect 方法中编写的绘制代码进行绘制。
原文地址: https://www.cveoy.top/t/topic/sVn 著作权归作者所有。请勿转载和采集!