addSubview动画仅在第二次后才能工作

时间:2012-08-23 07:46:01

标签: iphone objective-c ios

我正在尝试使用动画显示标签:

- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {
    if (isShowingRectangleLabel == NO) {
    [UIView transitionWithView:rectangleLabel duration:0.5
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^ { [self.view addSubview:rectangleLabel]; }
                    completion:nil];
        NSLog(@"action");
        isShowingRectangleLabel = YES;
    } else {
        [UIView transitionWithView:rectangleLabel duration:0.5
                       options:UIViewAnimationOptionTransitionFlipFromBottom
                    animations:^ { [rectangleLabel removeFromSuperview]; }
                    completion:nil];
        isShowingRectangleLabel = NO;
    }

}

但是这个动画只有在第二次添加到子视图后才能工作。我该如何解决?

编辑为了澄清,addSubview有效,但没有动画。

1 个答案:

答案 0 :(得分:3)

这样做:

- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {
    if (isShowingRectangleLabel == NO) {
    [UIView transitionWithView:self.view duration:0.5
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^ { [self.view addSubview:rectangleLabel]; }
                    completion:nil];
        NSLog(@"action");
        isShowingRectangleLabel = YES;
    } else {
        [UIView transitionWithView:self.view duration:0.5
                       options:UIViewAnimationOptionTransitionFlipFromBottom
                    animations:^ { [rectangleLabel removeFromSuperview]; }
                    completion:nil];
        isShowingRectangleLabel = NO;
    }

}