2014-02-07 50 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,继续前进,做动画,否则什么都不做。这将阻止推播前的突然最终动画。

更好的解决方案,以及一个我会很快实施,将与自动布局约束来代替我的使用框架。

相关问题