gpt4 book ai didi

iphone - iOS——drawRect 有时会在意想不到的大矩形上被调用

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

我有一个简单的测试应用程序。该应用程序由应用程序委托(delegate)、 View Controller 和 View 组成。应用程序委托(delegate)只是启动 View Controller 并将其 View 添加到窗口。 View Controller 依次在启动时加载 View 并且什么都不做。 View 如下所示:

@implementation MyView

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
}
return self;
}

- (void)drawRect:(CGRect)rect
{
static NSDate* prevDate = nil;
NSDate* now = [NSDate date];
NSString* timeString = @"";
if (prevDate) {
NSTimeInterval dt = [now timeIntervalSinceDate:prevDate];
timeString = [NSString stringWithFormat:@"%d seconds", (int)dt];
}
prevDate = now;
NSLog (@"drawRect %@ %@", NSStringFromCGRect(rect), timeString);
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
CGRect rect = CGRectMake(point.x, point.y, 40, 40);
[self setNeedsDisplayInRect:rect];
}

@end

经过一些修改,这是我的输出。我不明白的是 - 为什么有时会在整个屏幕上调用 drawRect 而不是只在小矩形上调用。

每次 drawRect 调用都会在触摸后立即进行。这符合预期。但为什么有些调用指定了大矩形?

2011-12-13 12:46:15.004 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 
2011-12-13 12:46:20.197 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 5 seconds
2011-12-13 12:46:23.235 DrawRectTest[1451:707] drawRect {{508, 475}, {40, 40}} 3 seconds
2011-12-13 12:46:27.671 DrawRectTest[1451:707] drawRect {{761, 484}, {40, 40}} 4 seconds
2011-12-13 12:46:36.394 DrawRectTest[1451:707] drawRect {{433, 254}, {40, 40}} 8 seconds
2011-12-13 12:46:51.190 DrawRectTest[1451:707] drawRect {{597, 270}, {40, 40}} 14 seconds
2011-12-13 12:47:04.979 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 13 seconds
2011-12-13 12:47:09.148 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 4 seconds
2011-12-13 12:47:14.314 DrawRectTest[1451:707] drawRect {{414, 480}, {40, 40}} 5 seconds
2011-12-13 12:47:31.343 DrawRectTest[1451:707] drawRect {{551, 503}, {40, 40}} 17 seconds
2011-12-13 12:47:33.639 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 2 seconds
2011-12-13 12:47:35.554 DrawRectTest[1451:707] drawRect {{521, 519}, {40, 40}} 1 seconds
2011-12-13 12:47:41.325 DrawRectTest[1451:707] drawRect {{366, 586}, {40, 40}} 5 seconds
2011-12-13 12:47:50.751 DrawRectTest[1451:707] drawRect {{535, 474}, {40, 40}} 9 seconds
2011-12-13 12:48:01.891 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 11 seconds
2011-12-13 12:49:24.600 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 82 seconds
2011-12-13 12:49:29.514 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 4 seconds
2011-12-13 12:49:30.774 DrawRectTest[1451:707] drawRect {{725, 372}, {40, 40}} 1 seconds
2011-12-13 12:50:04.435 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 33 seconds
2011-12-13 12:50:07.469 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 3 seconds
2011-12-13 12:50:09.417 DrawRectTest[1451:707] drawRect {{824, 471}, {40, 40}} 1 seconds
2011-12-13 12:51:04.550 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 55 seconds
2011-12-13 12:51:06.071 DrawRectTest[1451:707] drawRect {{0, 0}, {1024, 748}} 1 seconds
2011-12-13 12:51:07.809 DrawRectTest[1451:707] drawRect {{453, 557}, {40, 40}} 1 seconds
2011-12-13 12:51:11.776 DrawRectTest[1451:707] drawRect {{510, 370}, {40, 40}} 3 seconds

如果重要的话,我问的原因是我正在尝试提高大型项目的性能。缓慢的原因之一是那里有一些类似的 drawRect 调用。

最佳答案

在后续 drawRect 中更新或在该 View 中进行任何更多绘图时,不保证 UIView 会保存或重用其先前绘制的任何图形内容(先前的绘图已以只写方式发送到 GPU)。因此,任何绘图,即使是很小的一部分,都可能需要重新绘制整个 View 以重新创建该 View 的内容。

如果你只想绘制到一个小区域,你需要绘制到 UIView 以外的东西,例如你的应用程序分配的位图上下文。

关于iphone - iOS——drawRect 有时会在意想不到的大矩形上被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8494609/

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