Iphone翻页动画效果--CATransition实现

功能代码如下:

   在.h文件中定义

    NSMutableArray *views;

    NSInteger currentPage;

    CGPoint startPoint;

Ios代码   收藏代码
  1. - (void)viewDidLoad  //重写初始化载入方法  
  2. {  
  3.  /*  
  4.   初始化数据  
  5.   */  
  6.     [super viewDidLoad];  
  7.     self.view.userInteractionEnabled = YES;   //开启交互功能  
  8.     UIImageView *view1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pageone.png"]];  
  9.     view1.userInteractionEnabled = YES;  
  10.     view1.frame = CGRectMake(00320460); //设置坐标  
  11.     UIImageView *view2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pagetwo.png"]];  
  12.     view2.userInteractionEnabled = YES;  
  13.     view2.frame = CGRectMake(00320460);  
  14.     UIImageView *view3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pagethr.png"]];  
  15.     view3.userInteractionEnabled = YES;  
  16.     view3.frame = CGRectMake(00320460);  
  17.     UIImageView *view4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pagefou.png"]];  
  18.     view4.userInteractionEnabled = YES;  
  19.     view4.frame = CGRectMake(00320460);  
  20.     NSMutableArray *muView = [[NSMutableArray alloc] init];  
  21.     [muView addObject:view1];  
  22.     [muView addObject:view2];  
  23.     [muView addObject:view3];  
  24.     [muView addObject:view4];  
  25.     [view1 release];  
  26.     [view2 release];  
  27.     [view3 release];  
  28.     [view4 release];  
  29.     self.views = muView;  
  30.     [muView release];  
  31.       
  32.     [self.view addSubview:view1];   //添加视图  
  33.     //[self.view insertSubview:view1 atIndex:0];  
  34. }  
  35.   
  36. - (void)turnLeftPage:(UIView *)view  //自定义  
  37. {  
  38.    //设置type和subtype属性产生不同的动画效果  
  39.     CATransition *transition = [CATransition animation]; //定义过度动画  
  40.     transition.duration = 0.75; //持续时间  
  41.     transition.type = @"pageCurl";   //动画样式  
  42.     transition.subtype = kCATransitionFromLeft;  //方向  
  43.     [[self.view.subviews objectAtIndex:0] removeFromSuperview]; //移除原视图  
  44.     [self.view insertSubview:view atIndex:0];   //添加新视图  
  45. //    [self viewWillDisappear:NO];  
  46.     [self.view.layer addAnimation:transition forKey:nil]; //动画添加到层  
  47. }  
  48.   
  49.   
  50.   
  51. - (void)turnRightPage:(UIView *)view  
  52. {  
  53.     CATransition *transition = [CATransition animation];  
  54.     transition.duration = 0.75;  
  55.     transition.type = @"pageCurl";    
  56.     transition.subtype = kCATransitionFromRight;  
  57.     [[self.view.subviews objectAtIndex:0] removeFromSuperview];  
  58.     [self.view insertSubview:view atIndex:0];  
  59. //    [self viewWillDisappear:YES];  
  60.     [self.view.layer addAnimation:transition forKey:nil];  
  61. }  
  62.   
  63. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
  64. {  
  65.     UITouch *touch = [touches anyObject];  
  66.     CGPoint pointone = [touch locationInView:self.view];//获得初始的接触点  
  67.     self.startPoint  = pointone;  
  68.       
  69. }  
  70.   
  71. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  
  72. {  
  73.     UITouch *touch = [touches anyObject];  
  74.     CGPoint pointtwo = [touch locationInView:self.view];  //获得滑动后最后接触屏幕的点  
  75.     if(fabs(pointtwo.x-startPoint.x)>50){  //判断亮点间的距离  
  76.         if(pointtwo.x-startPoint.x>0){   //判断点的位置关系  
  77.             --currentPage;  
  78.             if(self.currentPage >= 0){   //判断页码  
  79.                 UIImageView *nextView = [self.views objectAtIndex:currentPage]; //初始化下一页  
  80.                 [self turnLeftPage:nextView];   //调用自定义方法  
  81.             }else{  
  82.                 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告!" message:@"已经到第一页了" delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];  
  83.                 [alert show]; //画警告  
  84.                 [alert release];  
  85.                 self.currentPage = 0;  
  86.                   
  87.             }  
  88.         }else{  
  89.             ++currentPage;  
  90.             if(self.currentPage < [self.views count]){  
  91.                 UIImageView *nextView = [self.views objectAtIndex:currentPage];  
  92.                 [self turnRightPage:nextView];  
  93.             }else{  
  94.                 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告!" message:@"已经是最后一页了" delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];  
  95.                 [alert show];  
  96.                 [alert release];  
  97.                 self.currentPage = [self.views count]-1;  
  98.             }  
  99.               
  100.         }  
  101.     }  
  102.       
  103. }  
这里使用了setType与setSubtype组合,这使用个比较保险,因为他的参数就是官方API里定义的,他们的参数说明可以参考如下: setType:可以返回四种类型: kCATransitionFade淡出 kCATransitionMoveIn覆盖原图 kCATransitionPush推出 kCATransitionReveal底部显出来 setSubtype:也可以有四种类型: kCATransitionFromRight; kCATransitionFromLeft(默认值) kCATransitionFromTop; kCATransitionFromBottom 还有一种设置动画类型的方法,不用setSubtype,只用setType [animation setType:@"suckEffect"]; 这里的suckEffect就是效果名称,可以用的效果主要有: pageCurl 向上翻一页 pageUnCurl 向下翻一页 rippleEffect 滴水效果 suckEffect 收缩效果,如一块布被抽走 cube 立方体效果 oglFlip 上下翻转效果  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值