2013-11-27 64 views
0

我無法在動畫之後更改我的按鈕文本。動畫UIButton更改文本更改位置

定義在我的.h文件

@property (weak, nonatomic) IBOutlet UIButton *btnCheck; 

我把這種方法時,我想動畫按鈕的位置變化

- (void)updateViews { 
    if(checkedIn){ 
     [UIView animateWithDuration:0.3f 
          delay:0.0f 
         options:UIViewAnimationOptionTransitionNone 
        animations:^{ 
         btnClear.hidden = YES; 
         [btnCheck setFrame:CGRectMake(90, btnCheck.frame.origin.y,  btnCheck.frame.size.width, btnCheck.frame.size.height)]; 
        } 
        completion:nil]; 

    }else{ 
     [UIView animateWithDuration:0.3f 
          delay:0.0f 
         options:UIViewAnimationOptionTransitionNone 
        animations:^{ 
         [btnCheck setFrame:CGRectMake(20, btnCheck.frame.origin.y, btnCheck.frame.size.width, btnCheck.frame.size.height)]; 
        } 
        completion:^(BOOL finished){ 
         btnClear.hidden = NO; 
        }]; 
    } 
    checkedIn = !checkedIn; 
} 

這一工程完美,圖片下方供您參考: Animation Animation

問題是我儘快更改bu的名字它會跳回原來的框架。

我改變在這樣的另一種方法的按鈕上的文字,

btnCheck.titleLabel.text = @ 「測試」;


而一旦我改變了按鈕跳回
test

是什麼導致按鈕框被重置? 我試圖在動畫之前,在完成之前更改它。它動畫到新的位置並跳回來。

回答

2

這是Autolayout導致的問題。禁用,一切正常

2

你不應該改變UIButton的標題。相反,嘗試使用setTitle:forState:方法設置它的標題。試試這個,讓我知道這是否有幫助,祝你好運!

+0

它給出了同樣的問題,我用了以下內容: [btnCheck setTitle:@「test」forState:UIControlStateNormal]; – user1838169

+0

然後它真的很奇怪,我強烈建議你仔細檢查你的代碼。 –

+0

根據你提供的圖像,我不認爲你的按鈕正在改變它的框架,也許你正在改變你的代碼中的'titleLabel'文本對齊? –

3

這將probabily不會回答你的問題,但我會告訴你一個更好的方式來改變一個框架,當你想在你的框架

CGRect buttonFrame = [btnCheck frame]; 
buttonFrame.origin.x = newXOrigin; 
[UIView animateWithDuration:0.3f 
        animations:^{ 
        [btnCheck setFrame:buttonFrame]; 
       }];  

我喜歡這種方法只改變一個屬性,因爲你不必創建新的CGRect並複製粘貼所有未修改的框架屬性。噢,你可能想從動畫塊中得到隱藏的任務,因爲這不能動畫

+0

謝謝,當時沒有想到。看起來更簡單。 – user1838169