Reputation: 2478
I have a UIViewController
that i edit using IB. I put a UINavigationBar
and a UISegmentedControl
on the top and 3 UIViews
under them. I want to be able to switch between the UIViews
using an animation, but i only want to animate the UIViews
, i want the navigationBar and athe segmentedControl to not move. I show the code how i do it now.
Any idea how i could only move the 3 views?
- (IBAction)segmentedControlValueChanged:(id)sender {
UISegmentedControl* segmentedControl = sender;
if(lastSelectedViewIndex != [segmentedControl selectedSegmentIndex]) {
CATransition *transition = [CATransition animation];
transition.duration = 0.4;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionMoveIn;
if(lastSelectedViewIndex < [segmentedControl selectedSegmentIndex])
transition.subtype = kCATransitionFromLeft;
else
transition.subtype = kCATransitionFromRight;
transition.removedOnCompletion = YES; // force removal of animation when completed.
{
switch ([segmentedControl selectedSegmentIndex]) {
case 0:
[self.usageScenarioView setHidden:NO];
[self.loginCredentialsView setHidden:YES];
[self.whatItCoversView setHidden:YES];
[self.pageControl setCurrentPage:0];
break;
case 1:
[self.usageScenarioView setHidden:YES];
[self.loginCredentialsView setHidden:NO];
[self.whatItCoversView setHidden:YES];
[self.pageControl setCurrentPage:1];
break;
case 2:
[self.usageScenarioView setHidden:YES];
[self.loginCredentialsView setHidden:YES];
[self.whatItCoversView setHidden:NO];
[self.pageControl setCurrentPage:2];
break;
}
}
lastSelectedViewIndex = [segmentedControl selectedSegmentIndex];
[self.view.layer addAnimation:transition forKey:nil];
}
}
Upvotes: 0
Views: 184
Reputation: 640
you can define a UIView* containerView in IB below your UISegmentedControl* segmentedControl which has the coordinates and position of the views you want. Then you can inside that view you can do transition between the three UIViews you want with this function:
-(void) replacePreviousViewInContainerViewWith:(UIView*) newView {
[UIView transitionWithView:_containerView duration:0.7
options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionCurveEaseIn
animations:^ {
[_containerView.subviews[0] removeFromSuperview];
[_containerView addSubview:newViewVC.view];
}
completion:^(BOOL finished) {
if (finished) {
NSLog(@"Now displaying %@.", [newView class]);
}
}];
[UIView commitAnimations];
}
and you call this function like this from where you want to change the Views:
UIViewController *newViewVC = [[UIViewController alloc] initWithNibName@"YOURNAME"];
newViewVC.view.frame = _containerView.frame;
[self replacePreviousViewInContainerViewWith: newViewVC.view];
Upvotes: 0
Reputation: 4257
Say your 3 views are named as view1, view2, view3. If you want to remove view1 and show view2 or view3, just do the existing code, but change
[self.view.layer addAnimation:transition forKey:nil];
into
[view1.layer addAnimation:transition forKey:nil];
that will animate the view1 only not the whole view. Similarly you can try,
[view2.layer addAnimation:transition forKey:nil];
[view3.layer addAnimation:transition forKey:nil];
more precisely, do like
transition.removedOnCompletion = YES; // force removal of animation when completed.
{
switch ([segmentedControl selectedSegmentIndex]) {
case 0:
[self.usageScenarioView setHidden:NO];
[self.loginCredentialsView setHidden:YES];
[self.whatItCoversView setHidden:YES];
[self.pageControl setCurrentPage:0];
[self.usageScenarioView.layer addAnimation:transition forKey:nil];
break;
case 1:
[self.usageScenarioView setHidden:YES];
[self.loginCredentialsView setHidden:NO];
[self.whatItCoversView setHidden:YES];
[self.pageControl setCurrentPage:1];
[self.loginCredentialsView.layer addAnimation:transition forKey:nil];
break;
case 2:
[self.usageScenarioView setHidden:YES];
[self.loginCredentialsView setHidden:YES];
[self.whatItCoversView setHidden:NO];
[self.pageControl setCurrentPage:2];
[self.whatItCoversView.layer addAnimation:transition forKey:nil];
break;
}
}
lastSelectedViewIndex = [segmentedControl selectedSegmentIndex];
}
Upvotes: 1