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

本站最新网址:http://www.helmsmansoft.com 欢迎关注

   功能代码如下:

   在.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;
            }
            
        }
    }
    
}
这里使用了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 上下翻转效果  

你可能感兴趣的:(transition)