jjzjj

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

coder 2024-01-13 原文

我有一个简单的测试应用程序。该应用程序由应用程序委托(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/

有关iphone - iOS——drawRect 有时会在意想不到的大矩形上被调用的更多相关文章

  1. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  2. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  3. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  4. ruby-on-rails - rbenv:从 RVM 移动到 rbenv 后,在 Jenkins 执行 shell 中找不到命令 - 2

    我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions

  5. ruby-on-rails - 找不到 gem railties (>= 0.a) (Gem::GemNotFoundException) - 2

    我已经看到了一些其他的问题,尝试了他们的建议,但没有一个对我有用。我已经使用Rails大约一年了,刚刚开始一个新的Rails项目,突然遇到了问题。我卸载并尝试重新安装所有Ruby和Rails。Ruby很好,但Rails不行。当我输入railss时,我得到了can'tfindgemrailties。我当前的Ruby版本是ruby2.2.2p95(2015-04-13修订版50295)[x86_64-darwin15],尽管我一直在尝试通过rbenv设置ruby​​2.3.0。如果我尝试rails-v查看我正在运行的版本,我会得到同样的错误。我使用的是MacOSXElCapitan版本10

  6. 即使安装了 gem,Ruby 也找不到所需的库 - 2

    我花了几天时间尝试安装ruby​​1.9.2并让它与gems一起工作:-/我最终放弃了我的MacOSX10.6机器,下面是我的Ubuntu机器上的当前状态。任何建议将不胜感激!#rubytest.rb:29:in`require':nosuchfiletoload--mongo(LoadError)from:29:in`require'fromtest.rb:1:in`'#cattest.rbrequire'mongo'db=Mongo::Connection.new.db("mydb")#gemwhichmongo/usr/local/rvm/gems/ruby-1.9.2-p0/g

  7. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  8. ruby - Sinatra 找不到 View 目录 - 2

    我正在尝试以一种更类似于普通RubyGem结构的方式构建我的Sinatra应用程序。我有以下文件树:.├──app.rb├──config.ru├──Gemfile├──Gemfile.lock├──helpers│  ├──dbconfig.rb│  ├──functions.rb│  └──init.rb├──hidden│  └──Rakefile├──lib│  ├──admin.rb│  ├──api.rb│  ├──indexer.rb│  ├──init.rb│  └──magnet.rb├──models│  ├──init.rb│  ├──invite.rb│  ├─

  9. ruby - 在 SUSE 上找不到 Ruby 的头文件? - 2

    我正在尝试在SUSEEnterprise11SP3上安装compass。我得到以下信息。有什么想法吗?geminstallcompassBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingcompass:ERROR:Failedtobuildgemnativeextension./usr/bin/rubyextconf.rbmkmf.rbcan'tfindheaderfilesforrubyat/usr/lib64/ruby/ruby.hextconffailed,exitcode1Gemfileswi

  10. ruby-on-rails - Heroku 找不到 SecureRandom - 2

    我的heroku应用崩溃了,因为它找不到模块“SecureRandom”。我在gemfile中指定了我的Ruby版本,我的计算机、gemfile和Heroku似乎都匹配Ruby版本号,尽管不是补丁号。其他帖子建议将usr/bin/heroku指向特定的Ruby文件,但我不确定该怎么做(我的应用程序中没有Herokubin)。这看起来像是Ruby版本错误。我该如何解决这个问题?classOrderItemHeroku日志:/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.3/lib/active_support/dependenci

随机推荐