这段代码有什么问题..(动画查看动画)?

时间:2012-06-21 04:41:02

标签: iphone objective-c ios animation uikeyboard

我不确定为什么会这样......

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

这两种方法都被调用......

这是代码..

- (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;
}

截至目前,我刚刚对数值进行了硬编码。

2 个答案:

答案 0 :(得分:1)

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

frame.origin.y -= 150;

frame.origin.y = 0;

答案 1 :(得分: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;
}