CATransition类动画

本文介绍了如何使用UIView和CATransition实现不同方向的翻转动画及多种过渡效果,包括淡入淡出、翻页、立方体翻转等,并展示了具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. -(void)leftClick{
  2. [UIViewbeginAnimations:nilcontext:nil];
  3. //displaymode,slowatbeginningandend
  4. [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];
  5. //动画时间
  6. [UIViewsetAnimationDuration:1.0f];
  7. //使用当前正在运行的状态开始下一段动画
  8. [UIViewsetAnimationBeginsFromCurrentState:YES];
  9. //给视图添加过渡效果
  10. [UIViewsetAnimationTransition:UIViewAnimationTransitionFlipFromLeftforView:imageViewcache:YES];
  11. [UIViewcommitAnimations];
  12. }
  13. -(void)rightClick{
  14. [UIViewbeginAnimations:nilcontext:nil];
  15. //displaymode,slowatbeginningandend
  16. [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];
  17. //动画时间
  18. [UIViewsetAnimationDuration:1.0f];
  19. //使用当前正在运行的状态开始下一段动画
  20. [UIViewsetAnimationBeginsFromCurrentState:YES];
  21. //给视图添加过渡效果
  22. [UIViewsetAnimationTransition:UIViewAnimationTransitionFlipFromRightforView:imageViewcache:YES];
  23. [UIViewcommitAnimations];
  24. }
  25. -(void)upClick{
  26. [UIViewbeginAnimations:nilcontext:nil];
  27. //displaymode,slowatbeginningandend
  28. [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];
  29. //动画时间
  30. [UIViewsetAnimationDuration:1.0f];
  31. //使用当前正在运行的状态开始下一段动画
  32. [UIViewsetAnimationBeginsFromCurrentState:YES];
  33. //给视图添加过渡效果
  34. [UIViewsetAnimationTransition:UIViewAnimationTransitionCurlUpforView:imageViewcache:YES];
  35. [UIViewcommitAnimations];
  36. }
  37. -(void)downClick{
  38. [UIViewbeginAnimations:nilcontext:nil];
  39. //displaymode,slowatbeginningandend
  40. [UIViewsetAnimationCurve:UIViewAnimationCurveEaseInOut];
  41. //动画时间
  42. [UIViewsetAnimationDuration:1.0f];
  43. //使用当前正在运行的状态开始下一段动画
  44. [UIViewsetAnimationBeginsFromCurrentState:YES];
  45. //给视图添加过渡效果
  46. [UIViewsetAnimationTransition:UIViewAnimationTransitionCurlDownforView:imageViewcache:YES];
  47. [UIViewcommitAnimations];
  48. }
  49. /*
  50. CATransition的type属性
  51. 1.#define定义的常量
  52. kCATransitionFade交叉淡化过渡
  53. kCATransitionMoveIn新视图移到旧视图上面
  54. kCATransitionPush新视图把旧视图推出去
  55. kCATransitionReveal将旧视图移开,显示下面的新视图
  56. 2.用字符串表示
  57. pageCurl向上翻一页
  58. pageUnCurl向下翻一页
  59. rippleEffect滴水效果
  60. suckEffect收缩效果,如一块布被抽走
  61. cube立方体效果
  62. oglFlip上下翻转效果
  63. */
  64. -(void)MyCAnimation1{
  65. CATransition*animation=[CATransitionanimation];
  66. //动画时间
  67. animation.duration=1.0f;
  68. //displaymode,slowatbeginningandend
  69. animation.timingFunction=UIViewAnimationCurveEaseInOut;
  70. //过渡效果
  71. animation.type=kCATransitionMoveIn;
  72. //过渡方向
  73. animation.subtype=kCATransitionFromTop;
  74. //添加动画
  75. [imageView.layeraddAnimation:animationforKey:nil];
  76. }
  77. -(void)MyCAnimation2{
  78. CATransition*animation=[CATransitionanimation];
  79. //动画时间
  80. animation.duration=1.0f;
  81. //displaymode,slowatbeginningandend
  82. animation.timingFunction=UIViewAnimationCurveEaseInOut;
  83. //在动画执行完时是否被移除
  84. animation.removedOnCompletion=NO;
  85. //过渡效果
  86. animation.type=@"pageCurl";
  87. //过渡方向
  88. animation.subtype=kCATransitionFromRight;
  89. //暂时不知,感觉与Progress一起用的,如果不加,Progress好像没有效果
  90. animation.fillMode=kCAFillModeForwards;
  91. //动画停止(在整体动画的百分比).
  92. animation.endProgress=0.7;
  93. [imageView.layeraddAnimation:animationforKey:nil];
  94. }
  95. -(void)MyCAnimation3{
  96. CATransition*animation=[CATransitionanimation];
  97. //动画时间
  98. animation.duration=1.0f;
  99. //displaymode,slowatbeginningandend
  100. animation.timingFunction=UIViewAnimationCurveEaseInOut;
  101. //过渡效果
  102. animation.type=@"pageUnCurl";
  103. //过渡方向
  104. animation.subtype=kCATransitionFromRight;
  105. //暂时不知,感觉与Progress一起用的,如果不加,Progress好像没有效果
  106. animation.fillMode=kCAFillModeBackwards;
  107. //动画开始(在整体动画的百分比).
  108. animation.startProgress=0.3;
  109. [imageView.layeraddAnimation:animationforKey:nil];
  110. }
  111. -(void)MyCAnimation4{
  112. [NSTimerscheduledTimerWithTimeInterval:3.0ftarget:selfselector:@selector(updateButterfly)userInfo:nilrepeats:YES];
  113. }
  114. -(void)updateButterfly{
  115. butterflyView.animationDuration=0.75f;
  116. [self.viewaddSubview:butterflyView];
  117. [butterflyViewstartAnimating];
  118. butterflyView.center=[butterflyViewrandomCenterInView:self.viewwithInset:10.0f];
  119. }
  120. CATransition比较强大,一般可以使用CATransition模拟UIView的动画。

    复制代码
    /*过渡效果
    fade//交叉淡化过渡(不支持过渡方向)
    push//新视图把旧视图推出去
    moveIn//新视图移到旧视图上面
    reveal//将旧视图移开,显示下面的新视图
    cube//立方体翻滚效果
    oglFlip//上下左右翻转效果
    suckEffect//收缩效果,如一块布被抽走(不支持过渡方向)
    rippleEffect//滴水效果(不支持过渡方向)
    pageCurl//向上翻页效果
    pageUnCurl//向下翻页效果
    cameraIrisHollowOpen//相机镜头打开效果(不支持过渡方向)
    cameraIrisHollowClose//相机镜头关上效果(不支持过渡方向)
    */

    /*过渡方向
    fromRight;
    fromLeft;
    fromTop;
    fromBottom;
    */
    CATransition*animation=[CATransitionanimation];
    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.layeraddAnimation:animationforKey:@"animation"];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值