Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop transitionFormViewController from animating viewWillAppear

I am trying to put together a simple container view controller that will allow me to present child view controllers from any direction (from the right like the standard navigation controller, put also from the top, bottom and left.

Developping for iOS 5, I use the parent/child view controller relationships and use a transitionFromViewController with a custom animation block to slide in the views.

All messages (viewWillAppear, etc.) are all passed on correctly to the child view controllers, but the problem is that for example viewWillAppear seems to be called from inside an animation loop (i.e. when I set animatable properties -like backgroundColor, or frame of subviews- of the view of the child view controller, they are actually animated). I do not want this behaviour, as I would like to use the viewWillAppear methods to perform initialization before showing the view controllers. I do not want this code to be animated.

Here is a simplified version of my code:

@implementation ContainerViewController
@synthesize currentViewController;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];

    self.currentViewController = [[SimpleViewController alloc] init];
    [self addChildViewController:self.currentViewController];
    self.currentViewController.view.frame = self.view.bounds;
    [self.view addSubview:self.currentViewController.view];
}

- (void) slideViewController {
    UIViewController* previousViewController = self.currentViewController;

    UIViewController* viewController = [[SimpleViewController alloc] init];
    viewController.view.frame = CGRectOffset(self.view.bounds, self.view.bounds.size.width, 0);
    [self addChildViewController:viewController];
    [previousViewController willMoveToParentViewController:nil];

    [self transitionFromViewController:previousViewController toViewController:viewController duration:1.0 options:UIViewAnimationOptionTransitionNone animations:^{
        viewController.view.frame = self.view.bounds;
    } completion:^(BOOL finished) {
        [previousViewController removeFromParentViewController];
        [viewController didMoveToParentViewController:self];
    }];

    self.currentViewController = viewController;
}

- (void) loop {
    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self slideViewController];
        [self loop];
    });
}

- (void)viewDidAppear:(BOOL)animated {
    [self loop];
}

@end


@implementation SimpleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewWillAppear:(BOOL)animated {
    CGFloat red = (arc4random()%256)/255.0;
    CGFloat green = (arc4random()%256)/255.0;
    CGFloat blue = (arc4random()%256)/255.0;
    self.view.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];
}

@end

If you try to run this code, you will see a new view controller slide from the right. The problem is that the backgroundColor of the view of that view controller gets animated from white to a random color (see viewWillAppear method of SimpleViewController). I would like that the backgroundColor of the view would be set immediately without being animated.

like image 457
quentinadam Avatar asked Jun 16 '26 19:06

quentinadam


1 Answers

I found a workaround, by disabling the animation of properties in the viewWillAppear method (see Disabling implicit animations in -[CALayer setNeedsDisplayInRect:] thread for details)

The code can be wrapped inside a CATransaction to commit changes without animation.

- (void)viewWillAppear:(BOOL)animated {
    [CATransaction begin];
    [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];

    CGFloat red = (arc4random()%256)/255.0;
    CGFloat green = (arc4random()%256)/255.0;
    CGFloat blue = (arc4random()%256)/255.0;

    self.view.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];

    [CATransaction commit];
}

However I wonder if anybody else has run into this problem and if there is a better method to solve it?

like image 106
quentinadam Avatar answered Jun 24 '26 21:06

quentinadam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!