ios开发视图切换效果动画类CATransition

来源:互联网 发布:oracle sql创建表 编辑:程序博客网 时间:2024/05/24 07:10

在网上找了一个Demo。觉得不错,这里拿来和大家分享一下。

http://code4app.com/ios/常用16种视图切换动画/500903b76803fa2f43000000

ios开发视图切换效果动画类CATransition - ♂苹果 - 眼睛想旅行
下边是视图切换时的部分代码:
 


- (IBAction)buttonPressed1:(id)sender {
    UIButton *button = (UIButton *)sender;
    NSInteger tag = button.tag;
    
    CATransition *animation = [CATransition animation];
    animation.delegate = self;
    animation.duration = kDuration;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
    
    NSLog(@"button tag is %i",tag);
    //上边按钮中的下边四个和下边的两行八个按钮,共12个button的tag
    switch (tag) {
        case 101://淡化
            animation.type = kCATransitionFade;
            break;
        case 102://推挤
            animation.type = kCATransitionPush;
            break;
        case 103://揭开
            animation.type = kCATransitionReveal;
            break;
        case 104://覆盖
            animation.type = kCATransitionMoveIn;
            break;
        case 201://立方体
            animation.type = @"cube";
            break;
        case 202://吸收
            animation.type = @"suckEffect";
            break;
        case 203://翻转
            animation.type = @"oglFlip";
            break;

        case 204://波纹
            animation.type = @"rippleEffect";
            break;
        case 205://翻页
            animation.type = @"pageCurl";
            break;
        case 206://反翻页
            animation.type = @"pageUnCurl";
            break;
        case 207://镜头开
            animation.type = @"cameraIrisHollowOpen";
            break;
        case 208://镜头关
            animation.type = @"cameraIrisHollowClose";
            break;
        default:
            break;
    }
    
    switch (self.typeID) {
        case 0://
            animation.subtype = kCATransitionFromLeft;
            break;
        case 1:
            animation.subtype = kCATransitionFromBottom;
            break;
        case 2:
            animation.subtype = kCATransitionFromRight;
            break;
        case 3:
            animation.subtype = kCATransitionFromTop;
            break;
        default:
            break;
    }
    self.typeID += 1;
    if (self.typeID > 3) {
        self.typeID = 0;
    }
    
    NSUInteger green = [[self.view subviews] indexOfObject:self.greenView];
    NSUInteger blue = [[self.view subviews] indexOfObject:self.blueView];
    [self.view exchangeSubviewAtIndex:green withSubviewAtIndex:blue];
    
    
    [[self.view layer] addAnimation:animation forKey:@"animation"];
    
    
}
#pragma mark UIView动画
- (IBAction)buttonPressed2:(id)sender {
    UIButton *button = (UIButton *)sender;
    NSInteger tag = button.tag;
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:kDuration];
    
     NSLog(@"button tag is %i",tag);
    
    //这里是最上边的四个按钮的tag
    switch (tag) {
        case 105://下翻
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
            break;
        case 106://上翻
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
            break;
        case 107://左转
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
            break;
        case 108://右转
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
            break;
        default:
            break;
    }
    
    NSUInteger green = [[self.view subviews] indexOfObject:self.greenView];
    NSUInteger blue = [[self.view subviews] indexOfObject:self.blueView];
    [self.view exchangeSubviewAtIndex:green withSubviewAtIndex:blue];
    
    [UIView setAnimationDelegate:self];
    // 动画完毕后调用某个方法
    //[UIView setAnimationDidStopSelector:@selector(animationFinished:)];
    [UIView commitAnimations];
}

这里定义了两个视图,一个greenView,一个blueView。切换,详情还是你们自已看Demo吧。

下边是找到的另一个相关的文章,就一起转来了,转自:http://www.cnblogs.com/pengyingh/articles/2339420.html 

UIViewAnimation动画与Core Animation的CATransition类动画
1.使用UIView类函数实现:
//UIViewAnimationTransitionFlipFromLeft, 向左转动
//UIViewAnimationTransitionFlipFromRight, 向右转动
//UIViewAnimationTransitionCurlUp, 向上翻动
//UIViewAnimationTransitionCurlDown, 向下翻动
[UIView beginAnimations:@"animationID" context:nil];
[UIView setAnimationDuration:0.5f]; //动画时长
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES]; //给视图添加过渡效果
//在这里写你的代码.
[UIView commitAnimations]; //提交动画
2.使用CATransition对象来实现:
CATransition比较强大,一般可以使用CATransition模拟UIView的动画。

    /* 过渡效果
fade     //交叉淡化过渡(不支持过渡方向)
push     //新视图把旧视图推出去
moveIn   //新视图移到旧视图上面
reveal   //将旧视图移开,显示下面的新视图
cube     //立方体翻滚效果
oglFlip  //上下左右翻转效果
suckEffect   //收缩效果,如一块布被抽走(不支持过渡方向)
rippleEffect //滴水效果(不支持过渡方向)
pageCurl     //向上翻页效果
pageUnCurl   //向下翻页效果
cameraIrisHollowOpen  //相机镜头打开效果(不支持过渡方向)
cameraIrisHollowClose //相机镜头关上效果(不支持过渡方向)
*/
 
/* 过渡方向
fromRight;
fromLeft;
fromTop;
fromBottom;
*/
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = 0.5f; //动画时长
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.fillMode = kCAFillModeForwards;
animation.type = @”cube”; //过度效果
animation.subtype = @”formLeft”; //过渡方向
animation.startProgress = 0.0 //动画开始起点(在整体动画的百分比)
animation.endProgress = 1.0;  //动画停止终点(在整体动画的百分比)
animation.removedOnCompletion = NO;
[self.view.layer addAnimation:animation forKey:@"animation"];

 
转自:http://www.cnblogs.com/project/archive/2011/09/27/2193556.html
 
实现iPhone漂亮的动画效果主要有两种方法:
  一种是UIView层面的,
  一种是使用CATransition进行更低层次的控制,
  第一种是UIView,UIView方式可能在低层也是使用CATransition进行了封装,它只能用于一些简单的、常用的效果展现,这里写一个常用的示例代码,供大家参考。
  Cpp代码
  [UIView beginAnimations:@"Curl"context:nil];//动画开始
  [UIView setAnimationDuration:0.75];
  [UIView setAnimationDelegate:self];
  [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:myview cache:YES];
  [myview removeFromSuperview];
  [UIView commitAnimations];
  第二种方式相对复杂一些,但如果更好的进行控制,还是使用这种方法吧,
  基本使用方法可以看一下如下例子:
  Cpp代码
  CATransition *animation = [CATransition animation];
  [animation setDuration:1.25f];
  [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
  [animation setType:kCATransitionReveal];
  [animation setSubtype: kCATransitionFromBottom];
  [self.view.layer addAnimation:animation forKey:@"Reveal"];
  这里使用了setType与setSubtype组合,这使用个比较保险,因为他的参数就是官方API里定义的,他们的参数说明可以参考如下:
  [animation setType:@"suckEffect"];
  这里的suckEffect就是效果名称,可以用的效果主要有:
  Cpp代码
  pageCurl 向上翻一页
  pageUnCurl 向下翻一页
  rippleEffect 滴水效果
  suckEffect 收缩效果,如一块布被抽走
  cube 立方体效果
  oglFlip 上下翻转效果
 
 
 

iphone中CABasicAnimation和UIView动画的区别[转]
关于UIView动画:

[UIView beginAnimations:@"zoom out" context:nil];
[UIView setAnimationDuration:1.f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
cover.transform = CGAffineTransformMakeScale(9.25,7.05);
cover.center = CGPointMake(430, 512);
[UIView commitAnimations]
UIView动画是应用在一个view上面的。
关于CABasicAnimation动画:

- (CAAnimation *)animationMove:(CGPoint)rootCenter
{
    CABasicAnimation *animationMove
    = [CABasicAnimation animationWithKeyPath:@"position"];
    animationMove.duration = 1;
    animationMove.autoreverses = NO;
//    animationMove.delegate = self;
    animationMove.removedOnCompletion = NO;
    animationMove.fillMode = kCAFillModeForwards;
    animationMove.fromValue = [NSValue valueWithCGPoint:self.oldCoverCenter];
    animationMove.toValue =[NSValue valueWithCGPoint:rootCenter];
 
    return animationMove;
}
CABasicAnimation动画是应用在一个layer上面的。
注:
1,把一个image放在一个view的layer上来放大的时候,如果用UIView来做,图片不会太多的失真和闪烁的效果,但是用CABasicAnimation来做失真和闪烁现象会很严重,效果很不好。
2,做 动画的叠加效果 很简单,只要把各自的动画放在一起就可以了。请看这个效果:一本书边移动到屏幕中间,边放大,边打开封面的效果。

[imageLayer addAnimation:[self animationOpen] forKey:@"Open"];
[UIView beginAnimations:@"zoom out" context:nil];
[UIView setAnimationDuration:1.f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
cover.transform = CGAffineTransformMakeScale(5.5,5.5);
cover.center = CGPointMake(629, 384);
[UIView commitAnimations];
 
- (CAAnimation *)animationOpen
{
    CABasicAnimation *animationOpen
    = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    animationOpen.duration = 1;
    animationOpen.autoreverses = NO;
    animationOpen.delegate = self;  //然后执行真正地打开书的内容
    animationOpen.removedOnCompletion = NO;
    animationOpen.fillMode = kCAFillModeForwards;
    animationOpen.fromValue = [NSNumber numberWithFloat:-M_PI/5];
    animationOpen.toValue = [NSNumber numberWithFloat:-M_PI/1.5];
 
    return animationOpen;
}
 

 

原创粉丝点击