3

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.

quentinadam
  • 3,058
  • 2
  • 27
  • 42
  • Can't you set the background color in the slideViewController method where you alloc init the SimpleViewController and set its view's frame? – rdelmar Aug 26 '12 at 00:16
  • I taught about that, but I would really like to keep the logic of being able to separate the code. In the child view controller I want to take care of initialising the view when it gets shown, and not in the container view controller. Also, in case I would I present the same child view controller class from different container view controller classes, I don't want to be duplicating the initialization code. – quentinadam Aug 26 '12 at 00:40

2 Answers2

2

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?

Community
  • 1
  • 1
quentinadam
  • 3,058
  • 2
  • 27
  • 42
0

After thinking about it some more, I agree with your comment, it would be better to keep all the logic for the child controller in its class. So, override init (or maybe viewDidLoad would work), in your child controller, and set any attributes you want there -- this should get things set up before your animations happen.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • That would work, but I would like to be able to perform some initialization code every time the view controller gets shown. Very much like the standard table view & navigation controller: when you select an item from the table, you see the details in a detail view controller. The standard practice (from Apple docs) is to reuse the same detail viewcontroller object and perform initialisation code inside the viewWillAppear. The issue here is that when using the custom view controller containment, the viewWillAppear methods is animated which looks funny whenever you start moving subviews around. – quentinadam Aug 26 '12 at 09:28