[問題] 關於UIView的setNeedsDisplay
我使用了Cooca的observer pattern讓UIView觀察自己的properties
當被觀察的properties改變時,便會要重新計算該UIView的frame/bounds
由於一個run loop內 可能會同時改變多個被觀察的properties
而導致在這個run loop內 需要多次計算新的frame/bounds
我希望能夠能夠在每個run loop結束前 才去判斷是否重新計算新的frame/bounds
於是我就是在UIView發現到properties被改變時 送了[self setNeedsDisplay]的訊息
可是這結果卻導致drawRect:的方法並沒有被呼叫
code大致如下:
*在initWithFrame:內 加入下面這行 以註冊觀察自己的property(font)
[self addObserver:self
forKeyPath:@"font"
options:NSKeyValueObservingOptionNew
context:NULL];
*在觀察到改變時
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context{
if ([keyPath isEqual:@"font"]) {
[self setNeedsDisplay];
}
}
這樣的方式 在我改變該UIView的font時 並沒有呼叫到drawRect:的方法
(註:該UIView一開始是使用CGRectZero當frame大小 然後改變ivar來決定最後的frame)
想請問一下是否我有哪邊搞錯了呢?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.83.65.55
→
10/31 00:26, , 1F
10/31 00:26, 1F
→
10/31 00:27, , 2F
10/31 00:27, 2F
→
10/31 00:51, , 3F
10/31 00:51, 3F
→
10/31 01:10, , 4F
10/31 01:10, 4F
→
10/31 01:11, , 5F
10/31 01:11, 5F
→
10/31 01:13, , 6F
10/31 01:13, 6F
→
10/31 01:14, , 7F
10/31 01:14, 7F
→
10/31 01:14, , 8F
10/31 01:14, 8F
→
10/31 01:17, , 9F
10/31 01:17, 9F
→
10/31 01:18, , 10F
10/31 01:18, 10F
→
10/31 01:19, , 11F
10/31 01:19, 11F
→
10/31 01:19, , 12F
10/31 01:19, 12F
→
10/31 01:23, , 13F
10/31 01:23, 13F
→
10/31 01:23, , 14F
10/31 01:23, 14F
→
10/31 01:27, , 15F
10/31 01:27, 15F
→
10/31 01:27, , 16F
10/31 01:27, 16F
→
10/31 01:33, , 17F
10/31 01:33, 17F
我目前只有查到如果UIView本身如果不是visible的話
就算送出了setNeedsDisplay 最後也不會呼叫drawRect:
但是我怎麼實作都不會呼叫 =.=
※ 編輯: leondemon 來自: 114.136.206.195 (10/31 01:35)
→
10/31 01:37, , 18F
10/31 01:37, 18F
→
10/31 01:50, , 19F
10/31 01:50, 19F
謝謝zonble大
我在TestView的- (void)_init內加入
self.backgroundColor = [UIColor redColor];
然後在TestView的- (void)drawRect:(CGRect)rect 內加入
CGSize newFrameSize = [@"Test" sizeWithFont:self.font];
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y,
newFrameSize.width, newFrameSize.height);
就會變成我要的
不過我的code就是沒辦法成功,我在研究看看囉...
※ 編輯: leondemon 來自: 111.83.137.34 (10/31 12:20)
→
10/31 12:21, , 20F
10/31 12:21, 20F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):
MacDev 近期熱門文章
PTT數位生活區 即時熱門文章
52
80