功能代码如下:
在.h文件中定义
NSMutableArray *views;
NSInteger currentPage;
CGPoint startPoint;
- - (void)viewDidLoad //重写初始化载入方法
- {
- /*
- 初始化数据
- */
- [super viewDidLoad];
- self.view.userInteractionEnabled = YES; //开启交互功能
- UIImageView *view1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pageone.png"]];
- view1.userInteractionEnabled = YES;
- view1.frame = CGRectMake(0, 0, 320, 460); //设置坐标
- UIImageView *view2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pagetwo.png"]];
- view2.userInteractionEnabled = YES;
- view2.frame = CGRectMake(0, 0, 320, 460);
- UIImageView *view3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pagethr.png"]];
- view3.userInteractionEnabled = YES;
- view3.frame = CGRectMake(0, 0, 320, 460);
- UIImageView *view4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pagefou.png"]];
- view4.userInteractionEnabled = YES;
- view4.frame = CGRectMake(0, 0, 320, 460);
- NSMutableArray *muView = [[NSMutableArray alloc] init];
- [muView addObject:view1];
- [muView addObject:view2];
- [muView addObject:view3];
- [muView addObject:view4];
- [view1 release];
- [view2 release];
- [view3 release];
- [view4 release];
- self.views = muView;
- [muView release];
- [self.view addSubview:view1]; //添加视图
- //[self.view insertSubview:view1 atIndex:0];
- }
- - (void)turnLeftPage:(UIView *)view //自定义
- {
- //设置type和subtype属性产生不同的动画效果
- CATransition *transition = [CATransition animation]; //定义过度动画
- transition.duration = 0.75; //持续时间
- transition.type = @"pageCurl"; //动画样式
- transition.subtype = kCATransitionFromLeft; //方向
- [[self.view.subviews objectAtIndex:0] removeFromSuperview]; //移除原视图
- [self.view insertSubview:view atIndex:0]; //添加新视图
- // [self viewWillDisappear:NO];
- [self.view.layer addAnimation:transition forKey:nil]; //动画添加到层
- }
- - (void)turnRightPage:(UIView *)view
- {
- CATransition *transition = [CATransition animation];
- transition.duration = 0.75;
- transition.type = @"pageCurl";
- transition.subtype = kCATransitionFromRight;
- [[self.view.subviews objectAtIndex:0] removeFromSuperview];
- [self.view insertSubview:view atIndex:0];
- // [self viewWillDisappear:YES];
- [self.view.layer addAnimation:transition forKey:nil];
- }
- - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- {
- UITouch *touch = [touches anyObject];
- CGPoint pointone = [touch locationInView:self.view];//获得初始的接触点
- self.startPoint = pointone;
- }
- - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- {
- UITouch *touch = [touches anyObject];
- CGPoint pointtwo = [touch locationInView:self.view]; //获得滑动后最后接触屏幕的点
- if(fabs(pointtwo.x-startPoint.x)>50){ //判断亮点间的距离
- if(pointtwo.x-startPoint.x>0){ //判断点的位置关系
- --currentPage;
- if(self.currentPage >= 0){ //判断页码
- UIImageView *nextView = [self.views objectAtIndex:currentPage]; //初始化下一页
- [self turnLeftPage:nextView]; //调用自定义方法
- }else{
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告!" message:@"已经到第一页了" delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
- [alert show]; //画警告
- [alert release];
- self.currentPage = 0;
- }
- }else{
- ++currentPage;
- if(self.currentPage < [self.views count]){
- UIImageView *nextView = [self.views objectAtIndex:currentPage];
- [self turnRightPage:nextView];
- }else{
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告!" message:@"已经是最后一页了" delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
- [alert show];
- [alert release];
- self.currentPage = [self.views count]-1;
- }
- }
- }
- }