簡體   English   中英

UILabel沒有正確居中

[英]UILabel not properly centering

當按下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)

實際坐標在此輸出中是當前的。 高亮標簽只是沒有移動到正確的位置! 再次,它目前閃爍,只是沒有居中。

例如,為什么不使用highlightedTextColor

label.highlightedTextColor = [UIColor redColor];

然后在你的觸摸事件中:

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

還沒有嘗試過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]];
                     }];
}

首先,您應該使用NSStringFromCGPoint函數打印出視圖中心。

第二 - 你確定你只在這個地方改變框架嗎? 動畫需要一秒鍾,所以如果你在其他地方寫了高亮位置,即使日志正確也不會居中。 您是否嘗試在動畫完成回調中記錄位置?

第三 - 你確定這兩個標簽是以同樣的方式創建的嗎? 文本對齊怎么樣? 字體大小怎么樣? 當你說“沒有正確居中”時,這是什么意思?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM