2012-05-28 44 views
0

當按下UILabel時,我正在製作一個「高光」UILabel(其中一個具有與按下的相同的框架和位置UILabel)應該閃爍。UILabel未正確居中

目前,高光標籤閃爍,但沒有正確對中。這裏是代碼:

-(void) flashRowViewController:(RowViewController*)rvc 
{ 
    NSLog(@"row center: (%f, %f)", rvc.rowLabel.center.x, rvc.rowLabel.center.y); 
    highlight.frame = rvc.rowLabel.frame; 
// highlight.center = rvc.rowLabel.center; // NSlog results are the same with or without this statement 
    NSLog(@"highlight center: (%f, %f)", highlight.center.x, highlight.center.y); 

    highlight.alpha = 1.0; 

    [UIView animateWithDuration:1.00 
        animations:^{ 
         highlight.alpha = 0.0; 
        }]; 
} 

我感到困惑。也許我只是看了太久,需要一個外部視角。下面是的NSLog的示例:

2012-05-28 10:40:41.603 RREF [89755:F803]行中心:(138.000000, 40.000000)

2012-05-28 10: 40:41.604 RREF [89755:F803]高亮中心: (138.000000,40.000000)

2012-05-28 10:40:43.538 RREF [89755:F803]行中心:(138.000000, 120.000000)

2012-05-28 10:40:43.538 RREF [89755:f803]突出顯示中心: (138.000000,120.000000)

2012-05-28 10:40:45.533 RREF [89755:F803]行中心:(138.000000, 200.000000)

2012-05-28 10:40:45.534 RREF [89755:F803]高亮中心: (138.000000,200.000000)

的實際座標是在這個輸出電流。重點標籤只是沒有移動到正確的位置!再次,它目前閃爍,只是沒有集中。

+0

他們都有相同的超級觀點嗎? –

+0

是的,他們都是同一個superview的成員。我也忘了提到高光標籤的位置不僅僅被一定的量所抵消,而且它不會在ALL上移動。 – Derek

+0

你確定'highlight'不是'nil'嗎? – deanWombourne

回答

2

爲什麼不使用highlightedTextColor所以例如

label.highlightedTextColor = [UIColor redColor]; 

,然後在觸摸事件:

[UIView animateWithDuration:1.0 
     animations:^(void) { 
      label.highlighted = YES; 
     } 
     completion:^(BOOL finished) { 
       label.highlighted = NO; 
     }]; 
0

沒有嘗試過iTukker的解決方案,但是我有另外一個很好的解決方案,這是動畫的背景顏色使用[label.layer setBackgroundColor:(CGColor)]

-(void) flashRowViewController:(RowViewController*)rvc 
{ 
    [rvc.rowLabel.layer setBackgroundColor:[highlightColor CGColor]]; 

    [UIView animateWithDuration:1.00 
        animations:^{ 
         [rvc.rowLabel.layer setBackgroundColor:[[UIColor clearColor] CGColor]]; 
        }]; 
} 
0

杉木st,你應該使用NSStringFromCGPoint函數打印出視圖中心。

第二 - 你確定你只是在這個地方改變框架?動畫需要一秒鐘,所以如果您在其他地方覆蓋突出顯示位置,即使日誌正確,它也不會居中。您是否嘗試在動畫完成回調中記錄位置?

第三 - 你確定這兩個標籤是以相同的方式創建的嗎?怎麼樣的文本對齊?字體大小呢?當你說「沒有正確對中」時,這意味着什麼?

Forth - 您可以使用動畫來更改第一個標籤的顏色/背景,您不需要兩個標籤。