0

我不知道为什么会这样。。

当用户开始在文本字段中输入时,我正在尝试为我的视图设置动画。但该代码仅适用于横向左方向,但不适用于横向右...

这些方法在两个方向都被调用......

这是代码..

- (void)keyboardWasShown:(NSNotification *)aNotification {
    if ( keyboardShown )
        return;

        NSTimeInterval animationDuration = 0.3;
        CGRect frame = self.view.frame;
        frame.size.width += 150;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];



    keyboardShown = YES;
}

- (void)keyboardWasHidden:(NSNotification *)aNotification {

        NSTimeInterval animationDuration = 0.3;
        CGRect frame = self.view.frame;
        frame.size.width -= 150;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];


    keyboardShown = NO;
}

到目前为止,我刚刚对值进行了硬编码..

4

2 回答 2

1

为什么不对视图的原点而不是大小设置动画?

frame.origin.y -= 150;

frame.origin.y = 0;
于 2012-06-21T05:10:20.883 回答
0

好的..在一些帮助下想通了...

当您设置视图的框架并将其控制器添加到窗口的根视图控制器时,您应该使用边界...。

因为窗口框架保持不变......

所以这是新代码的外观..

- (void)keyboardShown:(NSNotification *) aNotification {
    if ( keyboardShown )
        return;
    CGRect frame = self.view.bounds;
    frame.origin.y += 150;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:0.3];
    self.view.bounds = frame;
    [UIView commitAnimations];
    keyboardShown = NO;
}

- (void)keyboardHidden:(NSNotification *) aNotification {
    CGRect frame = self.view.bounds;
    frame.origin.y -= 150;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:0.3];
    self.view.bounds = frame;
    [UIView commitAnimations];
    keyboardShown = NO;
}
于 2012-06-21T07:33:18.260 回答