Matthew Clark
Matthew Clark

Reputation: 571

UIView frame animations in container break when adding children view controllers

So I have a Custom Container View Controller that I'm setting up and I have a custom animation that is suppose to happen when you swipe between the children view controllers that was working perfectly up until I took the temp UILabels out of the placeholder view and added the view from the child view controller to the place holder. If I comment out the "addSubview" lines then the animation works perfectly again, but obvious I don't have any children which is kinda the point...

Now I say that the animation doesn't work, but that's not entirely true because anything linked to something that I added at init (ie the notify and user placeholders and children) animate perfectly. The feed view is visable under them though and the header and footer bars only budge sometimes. The will randomly twitch if I move the touch the y axis as well as the x... it's weird.

init code for setting up and adding the child VCs:

UIStoryboard* sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    self.feedVC = (NiTActionFeedViewController*)[sb instantiateViewControllerWithIdentifier:@"Feed"];
    self.feedVC.delegate = self;
    [self addChildViewController:self.feedVC];
    self.feedVC.view.frame = self.mainPage.bounds;//mainPage view is the only UIView that is placed in the IB as template for the other views (so I don't have to worry about resizing for 4" retina^^;)
    [self.mainPage addSubview:self.feedVC.view];
    [self.feedVC didMoveToParentViewController:self];

    //notify
    self.notifyPage = [[UIView alloc] initWithFrame:CGRectOffset(self.mainPage.frame, -kPageShift, 0)];// this is one of the placeholder views
    [self.notifyPage setClipsToBounds:YES];

    self.notifyVC = (NiTNotificationViewController*)[sb instantiateViewControllerWithIdentifier:@"Notify"];
    self.notifyVC.delegate = self;
    [self addChildViewController:self.notifyVC];
    self.notifyVC.view.frame = self.notifyPage.bounds;
    [self.notifyPage addSubview:self.notifyVC.view];
    [self.notifyVC didMoveToParentViewController:self];

    //user
    self.userPage = [[UIView alloc] initWithFrame:CGRectOffset(self.mainPage.frame, kPageShift, 0)];
    [self.userPage setClipsToBounds:YES];

    self.userVC = (NiTUserViewController*)[sb instantiateViewControllerWithIdentifier:@"UserScreen"];
    self.userVC.delegate = self;
    [self addChildViewController:self.userVC];
    self.userVC.view.frame = self.userPage.bounds;
    [self.userPage addSubview:self.userVC.view];
    [self.userVC didMoveToParentViewController:self];

    [self.view addSubview:self.notifyPage];
    [self.view addSubview:self.userPage];

     //and then re-add the header and footer views so that the children don't cover them^^
    [self.view addSubview:self.headerbar];
    [self.view addSubview:self.headerButton];
    [self.view addSubview:self.footerbar];
    [self.view addSubview:self.footerButton];
    [self.view addSubview:self.rightPageIndicator];
    [self.view addSubview:self.leftPageIndicator];

and here is the touchesMoved code(if you need more then this let me know, but even on end they don't budge):

if (touches.count > 0) {
        UITouch* touch = [touches anyObject];
        CGPoint currentPoint = [touch locationInView:self.view];
        float diff = currentPoint.x - startingPoint.x;

        //currentScreen is an enum that I set on load and on trigger of the screen change so I always know what screen is showing and it is always accurate
        //this line is just to make sure we don't go past the "edge" of the screen and is working perfectly
        if ((self.currentScreen - (diff<0?-1:1)) > userScreen||(self.currentScreen - (diff<0?-1:1)) < notifScreen) {
            return;
        }


            self.headerbar.frame = CGRectOffset(initheaderbar, (int)(kHeaderBarShift * (diff/kPageShift)), 0);
            self.headerButton.frame = CGRectOffset(initheaderButton, kHeaderButtonShift * (diff/kPageShift), 0);
            self.footerbar.frame = CGRectOffset(initfooterbar, kFooterBarShift * (diff/kPageShift), 0);
            self.mainPage.frame = CGRectOffset(initmainPage, kPageShift * (diff/kPageShift), 0);
            self.userPage.frame = CGRectOffset(inituserPage, kPageShift * (diff/kPageShift), 0);
            self.notifyPage.frame = CGRectOffset(initnotifyPage, kPageShift * (diff/kPageShift), 0);

        //this always updates and looks right even though nothing changes on the screen
            NSLog(@"move main:%@(%@)\nnotify:%@\nuser:  %@",NSStringFromCGRect(CGRectOffset(initmainPage, kPageShift * (diff/kPageShift), 0)),NSStringFromCGRect(self.mainPage.frame),NSStringFromCGRect(CGRectOffset(initnotifyPage, kPageShift * (diff/kPageShift), 0)),NSStringFromCGRect(CGRectOffset(inituserPage, kPageShift * (diff/kPageShift), 0)));

            lastPoint = currentPoint;
    }else{
        //never seen this called thus why I have it here so I know if something mind blowing has happened
        NSLog(@"W.T.F");
    }

Now, the last piece you probably need to know is that I have a "touchController" protocol set up that can have "touchDelegates" set up in all the children that point to the parent so that any touches that they receive get forwarded to the Container VC and they are all forwarding properly so that's not it (and the animations wouldn't work at all if that was the case)...

Help me Obi Wan Kenobi, you're my only hope! Seriously, could use any help that anyone could give^^;

REQUESTED INFO I am pretty much animating everything on the screen. I can not post screenshots or a video because of the nature of the project, but hopefully you will be able to flow along with me with the diagram below(sorry, I'm not photoshop expect =P). The only thing not shown in the diagram is the headerButton, but basically it's just on top of the headerBar and moves at a slightly slower pace as it doesn't move as far. The idea of the design is that if you swipe in a direction it slides the screen in the opposite direction (like you would expect) and the header and footer bars slide at a different speed because they are shorter so the items on them are always visible, but have a "cool" effect in showing which side/page you are on. (neither bar ever shows an "end" on the screen, the "end" gets right to the edge of the screen, but never comes onto it) All of the constants are just that. kPageShift is the screen width (320). The rest are how far they need to move between screens, since they are all different sizes besides the pages. If you need more info please let me know!

enter image description here

Upvotes: 1

Views: 997

Answers (1)

raz0r
raz0r

Reputation: 3372

Turn off AutoLayout in your Storyboard file.

The constraints which have been setup are causing that random twitch which you talked about.

enter image description here

Upvotes: 3

Related Questions