我如何使用UIView animateWithDuration

时间:2018-03-14 07:34:01

标签: ios uiviewanimation

动画不起作用,立即调用完成块。如果我在完成块内注释代码,那么一切都按预期运行。

 [UIView animateWithDuration:0.3 animations:^{
        self.waitingAnchorAcceptView.alpha = 0;

    } completion:^(BOOL finished) {
        [self.waitingAnchorAcceptView removeFromSuperview];

    }];

2 个答案:

答案 0 :(得分:-1)

试试这个:

self.waitingAnchorAcceptView.alpha = 0;
[UIView animateWithDuration:0.3 animations:^{

  [self.view layoutIfNeeded];  

} completion:^(BOOL finished) {
    [self.waitingAnchorAcceptView removeFromSuperview];

}];

答案 1 :(得分:-1)

您可以通过创建新项目并在视图控制器中插入此代码来研究您所做或未做的事情:

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createButton];
}

- (void)createButton {

    UIButton *buttonThatWillHide = [[UIButton alloc] initWithFrame:CGRectMake(10.0 + arc4random()%100,
                                                                              100.0 + arc4random()%200, 120.0, 60.0)];
    buttonThatWillHide.alpha = 0.0;
    NSArray *colors = @[[UIColor redColor], [UIColor greenColor], [UIColor blueColor]];
    buttonThatWillHide.backgroundColor = colors[arc4random()%colors.count];
    [buttonThatWillHide setTitle:@"Press me" forState:UIControlStateNormal];
    [self.view addSubview:buttonThatWillHide];

    [buttonThatWillHide addTarget:self action:@selector(onButtonPress:) forControlEvents:UIControlEventTouchUpInside];

    [UIView animateWithDuration:0.5 animations:^{
        buttonThatWillHide.alpha = 1.0;
    }];
}

- (void)onButtonPress:(UIView *)sender {
    [UIView animateWithDuration:0.5 animations:^{
        sender.alpha = 0.0;
    } completion:^(BOOL finished) {
        [sender removeFromSuperview];
    }];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self createButton];
    });
}

@end
相关问题