iOS - 子视图不动画与帧更改

Mat*_*per 3 objective-c uiview ios

我有一个视图,我试图在键盘向上滑动时设置高度变化的动画(代码如下所示).我称之为框架改变的视图是完美的动画; 但是,该视图包含一个子视图,该子视图又包含一个文本字段(导致键盘弹出),这些子视图只是跳转到新的位置而不是动画.我在子视图上设置了自动布局约束,将其约束到超视图的左侧,底部和右侧,并保持恒定的高度.

示例视频:http://youtu.be/EmcZ-cXeTbM

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(slideViewForKeyboard:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(slideViewForKeyboard:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)slideViewForKeyboard:(NSNotification *)note {
    NSDictionary* userInfo = [note userInfo];

    // Get animation info from userInfo
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;    
    CGRect keyboardEndFrame;

    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];

    CGRect newFrame = CGRectMake(self.view.frame.origin.x,
                                 self.view.frame.origin.y,
                                 self.view.frame.size.width,
                                 keyboardEndFrame.origin.y);

    [UIView animateWithDuration:0 delay:0 options:(animationCurve << 16) animations:^{
        [self.view setFrame:newFrame];
    }completion:^(BOOL completed){}];
}
- (IBAction)endEditingOnTap:(UITapGestureRecognizer *)sender {
    [self.view endEditing:YES];
}

@end
Run Code Online (Sandbox Code Playgroud)

rob*_*off 9

您应该使用animationDuration动画块的持续时间.

除此之外,你有另一个问题.现在,您在动画块中设置视图的框架,因此更改是动画的.但是系统会在运行循环的布局阶段之后,在动画块之外放置子视图.这意味着子视图不会动画到新帧.

您可以通过发送layoutIfNeeded到动画块内的视图来解决这个问题:

[UIView animateWithDuration:animationDuration delay:0 options:(animationCurve << 16) animations:^{
    self.view.frame = newFrame;
    [self.view layoutIfNeeded];
} completion:^(BOOL completed){}];
Run Code Online (Sandbox Code Playgroud)

但是你可能会遇到另一个问题.您正在直接设置视图的框架,但您正在使用自动布局.在某些时候,自动布局可能会根据您的约束设置帧.您需要修改视图上的约束以控制其新帧,而不是直接设置框架.