<UI>实现动画的若干种方式

  IOS中实现动画有3中方式:基于UIView的动画、基于Layer的动画和利用第三方包HMGL实现的动画。

  1.实现基于UIView的动画:图片淡入淡出动画的例子(改变Alpha值)

- (void) alphaOut:(id)sender {
    // 准备动画
    [UIView beginAnimations:nil context:NULL];
    // 设置动画持续时间
    [UIView setAnimationDuration:5.0f];
    //要使视图 发生变化的相关代码
    [imageView0 setAlpha:0.0f];
    // 开始动画
    [UIView commitAnimations];
    // 这里打一条输出可以看出 程序在执行nslog的时候 动画还没有结束 所以动画是后台开一个线程自动执行
    NSLog(@"begin animation");
}

  另外可用这种方式实现的例子还有:移入移出动画(移动frame,例子省略)

  利用coreGraphics包中的CGAffine实现图片旋转动画

rotate
- (void) rotate:(id)sender {
    // 取得当前的旋转引子
    CGAffineTransform t = [imageView0 transform];
    // 在t旋转引子上在旋转PI/2角度
    CGAffineTransform newT = CGAffineTransformRotate(t, M_PI/2.0f);
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1.0f];
    //设置代理和下面的两个函数是为了在动画执行过程(开始、进行、结束)中让controller执行其他方法,
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView setAnimationWillStartSelector:@selector(animationWillStart:context:)];
    //另外两个进行回调的函数
    // 动画开始会调用[self start:]
    // 动画停止会调用[self finish:];
    [imageView0 setTransform:newT];
    [UIView commitAnimations];
}
// 动画完成回调函数
- (void) animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    NSLog(@"animation finish:%@", finished);
}
//动画将开始回调函数
- (void) animationWillStart:(NSString *)animationID context:(void *)context {
    NSLog(@"animation start");
}

  下面这个例子展示了UIView动画的形式控制

turnPageUp
- (void) turnPageUp:(id)sender {
    // 上翻页
    NSInteger imageViewIndex0 = [[parentView subviews] indexOfObject:imageView0];
    // 取得imageView0在父亲parentView重的index
    NSInteger imageViewIndex1 = [[parentView subviews] indexOfObject:imageView1];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:3.0f];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:parentView cache:YES];
    // UIViewAnimationTransitionCurlUp 上翻页
    // parentView 表示对parentView进行翻页
    // 换掉imageview0 和imageview1
    [parentView exchangeSubviewAtIndex:imageViewIndex0 withSubviewAtIndex:imageViewIndex1];
    [UIView commitAnimations];
}

  2.基于CALayer的动画实现

cube
- (void)cube:(id)sender {
    //需要导入的包
    // <QuartzCore/QuartzCore.h>
    // QuartzCore.framework
    //定义一个转场动画
    CATransition *animation = [CATransition animation];
    [animation setDuration:2.0f];
    //可用类型
    //kCATransitionFade 交叉 淡化过渡
    //kCATransitionMoveIn 新视图移到旧视图上边
    //kCATransitionPush新视图推出旧视图
    //kCATransitionReveal旧视图移除
    //其它 @"cube",@"pageCurl",@"pageUnCurl",@"rippleEffect",@"suckEffect",@"oglFlip"
    //设置类型为cube
    [animation setType:@"cube"];
    //设置动画子类型,图片从哪儿开始切换,底部
    [animation setSubtype:kCATransitionFromBottom];
    //交换parentView两个子视图
    [parentView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    //为parentView的layer添加设置好的动画,key是此动画的关键字
    [parentView.layer addAnimation:animation forKey:@"test"];
}

  3.利用第三方包实现HMGL动画

View Code
- (void)htmlClicked:(UIButton *)sender {
    // 使用HMGL做3D变换
    // 需要导入的包:OpenGLES.framework、QuartzCore.framework
    
    // 初始化了一个具体door动作,实现的动画是Cloth转场动作
    ClothTransition *door = [[ClothTransition alloc] init];
    //单例模式的一个例子,新建一个HMGL转场的管理器
    HMGLTransitionManager *manager = [HMGLTransitionManager sharedTransitionManager];
    // 设置使用哪个动画专场,添加到manager对象中
    [manager setTransition:door]; 
    [door release];
    // 对parentView进行动画
    [manager beginTransition:parentView]; 
    [parentView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    [manager commitTransition];
}

 

      

  

posted @ 2012-11-08 18:30  白条围巾  阅读(553)  评论(0编辑  收藏  举报