虽然,网上有很多代码,但是 还是觉得自己写了一个简单的动画翻转的效果,
会更加熟悉些。下面把代码贴出来学习学习。
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *flipButton=[[UIBarButtonItem alloc]
initWithTitle:@"翻转"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(flip:)];
self.navigationItem.rightBarButtonItem=flipButton;
fistView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
fistView.tag=100;
fistView.backgroundColor=[UIColor redColor];
[self.view addSubview:fistView];
secondView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
secondView.tag=101;
secondView.backgroundColor=[UIColor yellowColor];
[self.view addSubview:secondView];
}
-(void)flip:(id)sender{
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:[self.view viewWithTag:100]];
NSInteger seconde= [[self.view subviews] indexOfObject:[self.view viewWithTag:101]];
[self.view exchangeSubviewAtIndex:fist withSubviewAtIndex:seconde];
//当父视图里面只有两个视图的时候,可以直接使用下面这段.
//[self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
本文介绍了一种在iOS应用中实现视图翻转动画的方法。通过创建两个颜色不同的视图,并利用UIKit提供的动画API实现翻转效果。文章提供了完整的Swift代码示例,包括设置按钮触发翻转动画、调整动画曲线及时长等。
839

被折叠的 条评论
为什么被折叠?



