2014-02-07 45 views
0

簡短說明:當鍵盤顯示時,我翻譯了一個按鈕,但點擊按鈕後,它會回彈到原來的位置。爲什麼單擊動畫UIButton的位置時會重置?

長描述:我有兩個按鈕,一個在另一個之上。一個隱藏,直到滿足某些標準,然後它變得可見,另一個隱藏。

@property (weak, nonatomic) IBOutlet UIButton *skipButton; 
@property (weak, nonatomic) IBOutlet UIButton *saveButton; 
@property (assign, nonatomic) BOOL saveButtonEnabled; 

@property (assign, readonly, nonatomic) CGRect fieldContainerDefaultFrame; 
@property (assign, readonly, nonatomic) CGRect skipButtonDefaultFrame; 

當視圖加載我緩存的缺省位置

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    ... 

    _fieldContainerDefaultFrame = self.fieldContainerView.frame; 
    _skipButtonDefaultFrame = self.skipButton.frame; 

    [self.saveButton setHidden:YES]; 
    self.saveButtonEnabled = NO; 
} 

按鈕被正確連接到插座,這些都是他們的「潤色內部」行動

#pragma mark - Actions 

- (IBAction)didPressSaveButton:(id)sender 
{ 
    // Code here to persist data 

    // Code here to push segue to next scene 
} 

- (IBAction)didPressSkipButton:(id)sender 
{ 
    // Code here to push segue to next scene 
} 

這裏有方法響應鍵盤通知。我註冊了通知,我只是不包括該代碼。 (有沒有的問題出現,所有這些東西被正確運行。)

注:兩個按鈕都包含在同一個「容器」框(與其他字段一起),換算起來。按鈕翻譯了額外的金額。翻譯一般來說是唯一的問題,就是單擊按鈕時重置行爲。

#pragma mark - Notification handlers 

- (void)handleKeyboardWillShowNotification:(NSNotification *)note 
{ 
    if([self.bioTextView isFirstResponder]) 
    { 
     CGRect newFieldContainerFrame = self.fieldContainerDefaultFrame; 
     CGRect newSkipButtonFrame = self.skipButtonDefaultFrame; 
     newFieldContainerFrame.origin.y += AGSTSignupDetailsFieldContainerEditingOffset; 
     newSkipButtonFrame.origin.y += AGSTSignupDetailsSkipButtonEditingOffset; 

     NSTimeInterval duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 
     UIViewAnimationCurve animationCurve = [note.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]; 

     [UIView animateWithDuration:duration animations:^{ 

      [UIView setAnimationCurve:animationCurve]; 
      self.fieldContainerView.frame = newFieldContainerFrame; 
      self.skipButton.frame = newSkipButtonFrame; 
      self.saveButton.frame = newSkipButtonFrame; 
     }]; 
    } 
} 

- (void)handleKeyboardWillHideNotification:(NSNotification *)note 
{ 
    NSTimeInterval duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 
    UIViewAnimationCurve animationCurve = [note.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]; 

    [UIView animateWithDuration:duration animations:^{ 

     [UIView setAnimationCurve:animationCurve]; 
     self.fieldContainerView.frame = self.fieldContainerDefaultFrame; 
     self.skipButton.frame = self.skipButtonDefaultFrame; 
     self.saveButton.frame = self.skipButtonDefaultFrame; 
    }]; 
} 

我很感激任何關於如何解決錯誤或如何制定更好的解決方案的見解或建議。謝謝。

+0

您是否使用自動佈局?的 –

+0

可能重複的[爲什麼的UIButton幀由復位的setTitle:forState:方法](http://stackoverflow.com/questions/17997163/why-uibutton-frame-is-reset-by-settitle-forstate-method) –

+0

是,我正在使用自動佈局。當我跑出門時,我發佈了這個消息,我錯過了一些信息。我將在大約十個小時內坐下來徹底解決這個問題。我在想推push segue可能會導致鍵盤消失,導致平移運行,但隨後push segue可能已經取消了所有未決的動畫,因爲有新的VC進入。但是rob的指針指向約束與幀的問題也很有趣。感謝提示,我今晚會回來。 – bgfriend0

回答

0

好吧,我解決了這一問題。雖然我很欣賞爲更好的做法(即,使用自動佈局時不設置幀),這是不是在我的情況下,問題的真正原因指示搶mayoff的迴應,因此,我不認爲我的Q & A是一我能找到的任何東西的副本。如果有人遇到類似的問題,我會發布這個答案。我猜,按鈕的push segue做了兩件互不相容的事情:(1)它導致鍵盤消失,導致字段向下轉換; (2)當新的VC出現時,它引起了整個場景的動畫,這顯然取消了所有未決的動畫(即,翻譯),並導致這些翻譯突然跳到它們的目的地狀態。

最快的解決辦法是設置一個BOOL didPressSaveButton到沒有對viewDidLoad中,只將其設置爲YES一旦按下按鈕,然後每一個相關的翻譯動畫前檢查BOOL:如果BOOL是NO,繼續前進,做動畫,否則什麼都不做。這將阻止推播前的突然最終動畫。

更好的解決方案,以及一個我會很快實施,將與自動佈局約束來代替我的使用框架。

相關問題