简体   繁体   English

未在UIView动画中调用Completion ^块

[英]Completion^ block not being called in UIView animation

Is there something I'm missing here? 我在这里想念什么吗? i want the imageview to slide in then slide out from the bottom of the screen. 我希望imageview滑入,然后从屏幕底部滑出。

also, this seems to put the UIImageView behind the navigation bar how can I make a CGRect to fit the screen under the navbar? 此外,这似乎将UIImageView放在导航栏的后面,如何制作CGRect使其适合导航栏下的屏幕?

_finga = [[UIImageView alloc] initWithFrame:CGRectMake(0, 88, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-88)];
hiddenFrame = CGRectOffset(_finga.frame, 0, _finga.frame.size.height);
_finga.frame = hiddenFrame;
[self.view addSubview:_finga];
_finga.image = [UIImage imageNamed:@"Finga"];
[UIView animateWithDuration:2.0 animations:^{
    _finga.frame = self.view.bounds;
}completion:^(BOOL done) {
    if (done) {
        NSLog(@"Complete!");
        [UIView animateWithDuration:2.0 animations:^{
            _finga.frame = hiddenFrame;
        }];
    }
}];

The CGRect you initialize _finga with would put it under the nav bar. 用来初始化_finga会将其放在导航栏下面。 In the animation, you are setting the frame to the bounds of the view, which would put it behind the bar, since the y value would be 0. 在动画中,您正在将框架设置为视图的边界,这会将其置于条形后面,因为y值将为0。

You could write less code by initializing _finga with this frame from the start: 您可以从一开始就通过使用此框架初始化_finga来编写更少的代码:

CGRectMake(0,
           self.view.bounds.size.height,
           self.view.bounds.size.width,
           self.view.bounds.size.height-88);

That will put the view off the screen. 这将使视图离开屏幕。 Then after you add it as a subview and set its image, animate the view back up to this frame: 然后,将其添加为子视图并设置其图像后,将视图动画化回此框架:

CGRectMake(0,
           88,
           self.view.bounds.size.width,
           self.view.bounds.size.height-88);

Which will put the view just below the nav bar. 这会将视图置于导航栏的正下方。 Consider also replacing all the hard coded 88s with a variable or #define so that you can play around with it more easily and in case the nav bar height ever changes. 还考虑将所有硬编码的88s替换为变量或#define这样您就可以更轻松地使用它,以防导航栏高度发生变化。

As for the completion block, try logging that @"Complete!" 至于完成块,请尝试记录@"Complete!" string before you check if done is YES , or put a breakpoint there and see what the value of done is. 在检查done是否为YES之前输入字符串,或在该处放置断点以查看done的值是done Your animation may not be completing for some reason, which would explain why the code in the completion block is not being run. 您的动画可能由于某种原因未完成,这可以解释为什么未运行完成块中的代码。

Generally, though, if you are just using the completion block to run another animation after the first one, you don't need to check the done BOOL at all. 但是,通常,如果您仅使用完成块在第一个动画之后运行另一个动画,则根本不需要检查已完成的BOOL It's only crucial when something else in your program depends on the state of the animation. 仅当程序中的其他内容取决于动画的状态时,这才至关重要。 For example, the user may click a button which animates something and then takes the user to a different section of the app. 例如,用户可以单击将某些内容设置为动画的按钮,然后将用户带到应用程序的其他部分。 But if the user cancels the animation, you may not want to go to the other section after all, so you can check done . 但是,如果用户取消动画,则您可能根本不希望转到其他部分,因此可以检查done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM