ios 动画 UIView 向下滑动,然后向上滑动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20529614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:43:56  来源:igfitidea点击:

Animating a UIView to slide down, then slide up

iosobjective-canimationslider

提问by Stonep123

Im trying to figure out why this isnt working

我想弄清楚为什么这不起作用

in my tableViewcontroller viewDidLoad

在我的 tableViewcontroller viewDidLoad

self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 320,0)];

self.headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 320, 0)];

self.headerLabel.textAlignment = NSTextAlignmentCenter;

self.headerLabel.text = @"text";

[self.view addSubview:self.headerView];


[self.headerView addSubview:self.headerLabel];




[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{

    self.headerLabel.frame = CGRectMake(0, 5, 320,15);
    self.headerView.frame  = CGRectMake(0, 5, 320,15);

} completion:^(BOOL finished) {

    [UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{

        self.headerLabel.frame = CGRectMake(0, 5, 320,0);
        self.headerView.frame  = CGRectMake(0, 5, 320,0);

    } completion:^(BOOL finished) {

    }];

}];

if I remove the slide back up part in the completion block of the first animate call It works. The view slides down correctly. However I cannot get it to shrink back up at all. When I include the slide up code in the completion block the view is not displayed at all on load and I dont know why and Im going insane

如果我删除第一个动画调用的完成块中的幻灯片备份部分,它会起作用。视图正确向下滑动。但是我根本无法让它缩小。当我在完成块中包含向上滑动代码时,视图在加载时根本不显示,我不知道为什么,我快疯了

回答by rdelmar

I'm not sure why the label disappears, but you can fix that by giving the view and label an appropriate height when you create them, and only animate the label's y position rather than its height.

我不确定标签为什么会消失,但是您可以通过在创建视图和标签时为它们提供适当的高度来解决这个问题,并且只对标签的 y 位置而不是其高度进行动画处理。

- (void)viewDidLoad {
    [super viewDidLoad];

    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -30, 320,30)];
    self.headerView.backgroundColor = [UIColor yellowColor];
    self.headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 320, 21)];

    self.headerLabel.textAlignment = NSTextAlignmentCenter;

    self.headerLabel.text = @"text";

    [self.view addSubview:self.headerView];
    [self.headerView addSubview:self.headerLabel];

    [UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.headerView.frame  = CGRectMake(0, 0, 320,30);
    } completion:^(BOOL finished) {

        [UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
            self.headerView.frame  = CGRectMake(0, -30, 320,30);

        } completion:^(BOOL finished) {

        }];

    }];
}

回答by tjboneman

viewDidLoad is not really the best place to be starting animations. You should move the code to viewWillAppear: , and if you only want this to occur the first time the view appears, you should add a BOOL property to your controller (i.e. self.hasperformedInitialHeaderAnimation), so:

viewDidLoad 并不是开始动画的最佳位置。您应该将代码移动到 viewWillAppear: ,如果您只想在第一次出现视图时发生这种情况,您应该向您的控制器添加一个 BOOL 属性(即 self.hasperformedInitialHeaderAnimation),因此:

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if(!self.hasPerformedInitialHeaderAnimation){
self.hasPerformedInitalHeaderAnimation = YES;
[UIView animateWithDuration:.5 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{

self.headerLabel.frame = CGRectMake(0, 5, 320,15);
self.headerView.frame  = CGRectMake(0, 5, 320,15);

} completion:^(BOOL finished) {

[UIView animateWithDuration:.5 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{

    self.headerLabel.frame = CGRectMake(0, 5, 320,0);
    self.headerView.frame  = CGRectMake(0, 5, 320,0);

} completion:^(BOOL finished) {

}];
}