文章首发自点击打开链接
这里介绍如何进行场景旋转转换,分四部分介绍。
1.控制器的切换
SecondViewController *second = [[SecondViewController alloc] init];
//这里是一个枚举值,里面列出了4种动画,第一种为默认的
[second setModalTransitionStyle:(UIModalTransitionStyleFlipHorizontal)];
[self presentViewController:second animated:YES completion:nil];
2.UIView的切换
UIView *backview = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 200, 200)];
[backview setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:backview];
UIView *topview = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 200, 200)];
[topview setBackgroundColor:[UIColor yellowColor]];
[self.view addSubview:topview];
//这里做了一个延迟,实际中忽略
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//不同于UIView 的Animation方法,这里使用的是transition方法来实现UIView的切换
[UIView transitionFromView:topview toView:backview duration:1 options:(UIViewAnimationOptionTransitionFlipFromLeft) completion:nil];
});
3.利用CoreGraphics进行切换
CGContextRef context=UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
//这里时查找视图里的子视图(这种情况查找,可能时因为父视图里面不只两个视图)
NSInteger fist= [self.view.subviews indexOfObject:view];
NSInteger seconde= [self.view.subviews indexOfObject:view1];
[self.view exchangeSubviewAtIndex:fist withSubviewAtIndex:seconde];
//当父视图里面只有两个视图的时候,可以直接使用下面这段.
//[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
使用完上面三种后会发现,虽然都实现了界面和场景的旋转切换,但是都有一个弊端,就是在旋转的过程中,整个程序也在旋转。
这里解决这种方式的方式可能很简单粗暴,就是将以上两种方式结合。在父controller上面加一个子controller,子controller里面添加需要进行界面切换的两个UIView,从而实现部分的切换。下面看代码
4.部分切换
-(void)beginAnimation
{
CGContextRef context=UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
//这里时查找视图里的子视图(这种情况查找,可能时因为父视图里面不只两个视图)
NSInteger fist= [self.view.subviews indexOfObject:view];
NSInteger seconde= [self.view.subviews indexOfObject:view1];
[self.view exchangeSubviewAtIndex:fist withSubviewAtIndex:seconde];
//当父视图里面只有两个视图的时候,可以直接使用下面这段.
//[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
isfound = !isfound;
view.hidden = !isfound;
view1.hidden = isfound;
}