iOS CGAffineTransform 动画

举报
福州司马懿 发表于 2021/11/19 04:44:41 2021/11/19
【摘要】 CoreGraphics框架中的CGAffineTransform类可用于设定UIView的transform属性,控制视图的缩放、旋转和平移操作: 另称放射变换矩阵,可参照线性代数的矩阵实现方式 这里附上的CGAffineTransform官方文档: https://developer.apple.com/library/ios...

CoreGraphics框架中的CGAffineTransform类可用于设定UIView的transform属性,控制视图的缩放、旋转和平移操作:

另称放射变换矩阵,可参照线性代数的矩阵实现方式

这里附上的CGAffineTransform官方文档:

https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGAffineTransform/index.html

Transform 是一种状态,并且只有一种状态

(1)CGAffineTransformMakeTranslation(<#CGFloat tx#>, <#CGFloat ty#>):只能变化一次,因为这种方式的变化始终是以最原始的状态值进行变化的,所以只能变化一次

(2)CGAffineTransformTranslate(CGAffineTransform t, <#CGFloat tx#>, <#CGFloat ty#>):能够多次变化,每次变化都是以上一次的状态(CGAffineTransform t)进行的变化,所以可以多次变化

(3) CGAffineTransformIdentity:清空所有的设置的transform(一般和动画配合使用,只能使用于transfofrm设置的画面)

(4)CGAffineTransformMakeScale( CGFloat  sx,  CGFloat  sy) (缩放:设置缩放比例)仅通过设置缩放比例就可实现视图扑面而来和缩进频幕的效果。

(5) CGAffineTransformMakeRotation( CGFloat  angle) (旋转:设置旋转角度)

关键代码如下:


  
  1. @implementation ViewController
  2. @synthesize imageview1,imageview2, label1, imageContainView;
  3. UIImage *image1, *image2;
  4. UIView *view1, *view2;
  5. - (IBAction)button1_click:(id)sender {
  6. //翻转动画,旋转360度后复原(doflip)
  7. // additional context info passed to will start/did stop selectors. begin/commit can be nested
  8. //注意:动画效果是setAnimationTransition控制的,beginAnimations只是标识了这一组动画的名称
  9. [UIView beginAnimations:@"doflip" context:nil];
  10. [UIView setAnimationDuration:2];
  11. /*
  12. 动画速度
  13. typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {
  14. UIViewAnimationCurveEaseInOut, // slow at beginning and end
  15. UIViewAnimationCurveEaseIn, // slow at beginning
  16. UIViewAnimationCurveEaseOut, // slow at end
  17. UIViewAnimationCurveLinear
  18. };
  19. */
  20. [UIView setAnimationCurve:UIViewAnimationCurveLinear];
  21. [UIView setAnimationDelegate:self];
  22. //设置翻转动画的起始方向
  23. //UIViewAnimationTransitionFlipFromLeft(从左到右)
  24. //UIViewAnimationTransitionFlipFromLeft(从右到左)
  25. [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:imageContainView cache:YES];
  26. // starts up any animations when the top level animation is commited
  27. [UIView commitAnimations];
  28. }
  29. - (IBAction)button2_click:(id)sender {
  30. //旋转动画,保持旋转后的状态(rotate)
  31. CGAffineTransform transform = CGAffineTransformRotate(imageContainView.transform, M_PI/6.0);
  32. [UIView beginAnimations:@"rotate" context:nil];
  33. [UIView setAnimationDuration:2];
  34. [UIView setAnimationDelegate:self];
  35. [imageContainView setTransform:transform];
  36. [UIView commitAnimations];
  37. }
  38. - (IBAction)button3_click:(id)sender {
  39. //偏移动画(move)
  40. [UIView beginAnimations:@"move" context:nil];
  41. [UIView setAnimationDuration:2];
  42. [UIView setAnimationDelegate:self];
  43. imageContainView.frame = CGRectMake(80, 80, 200, 200);
  44. [label1 setBackgroundColor:[UIColor yellowColor]];
  45. [label1 setTextColor:[UIColor redColor]];
  46. [UIView commitAnimations];
  47. }
  48. - (IBAction)button4_click:(id)sender {
  49. //翻页动画(curlUp 和 curlDown)
  50. //curlUp表示将这一页翻上去
  51. //curlDown表示将上一页翻下来
  52. //注意:翻页动画针对的是与屏幕平行的矩形,如果被旋转相应角度,翻页不正常
  53. [UIView beginAnimations:@"xxx" context:nil];
  54. [UIView setAnimationDuration:2];
  55. [UIView setAnimationDelegate:self];
  56. [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:imageContainView cache:YES];
  57. NSInteger imageview1Index = [[imageContainView subviews] indexOfObject:imageview1];
  58. NSInteger imageview2Index = [[imageContainView subviews] indexOfObject:imageview2];
  59. NSLog(@"subview index %ld %ld", imageview1Index, imageview2Index);
  60. //交换2视图的位置
  61. [imageContainView exchangeSubviewAtIndex:imageview2Index withSubviewAtIndex:imageview1Index];
  62. [UIView commitAnimations];
  63. }
  64. - (IBAction)button5_click:(id)sender {
  65. //缩放动画(scale)
  66. //Scale `t' by `(sx, sy)',缩放默认是以视图的中心点为轴的
  67. CGAffineTransform transform = CGAffineTransformScale(imageContainView.transform, 1.2, 1.2);
  68. [UIView beginAnimations:@"scale" context:nil];
  69. [UIView setAnimationDuration:2];
  70. [UIView setAnimationDelegate:self];
  71. [imageContainView setTransform:transform];
  72. [UIView commitAnimations];
  73. }
  74. - (IBAction)button6_click:(id)sender {
  75. //根据当前的动画取反(Invert)
  76. //注意:只针对setTransform的视图,并且是取反而不是还原,其它动画无法取反
  77. CGAffineTransform transform = CGAffineTransformInvert(imageContainView.transform);
  78. [UIView beginAnimations:@"Invert" context:nil];
  79. [UIView setAnimationDuration:2];
  80. [UIView setAnimationDelegate:self];
  81. [imageContainView setTransform:transform];
  82. [UIView commitAnimations];
  83. }
  84. - (void)startAnimation:(NSString*) transitionType {
  85. CATransition *transition = [CATransition animation];
  86. transition.delegate = self;
  87. transition.duration = 2;
  88. //时间函数(控制动画速度)
  89. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
  90. /* The name of the transition. Current legal transition types include
  91. * `fade', `moveIn', `push' and `reveal'. Defaults to `fade'. 另外还有一些私有动画 “pageCurl”(向上翻页) “pageUnCurl”(向下翻页)、"rippleEffect"(水滴) "suckEffect"(收缩抽走) "cube"(立方体) "oglFilp"(上下翻转) "cameraIrisHollowOpen"(镜头开) "cameraIrisHollowClose"(镜头关)*/
  92. transition.type = transitionType;
  93. /* An optional subtype for the transition. E.g. used to specify the
  94. * transition direction for motion-based transitions, in which case
  95. * the legal values are `fromLeft', `fromRight', `fromTop' and
  96. * `fromBottom'. */
  97. transition.subtype = kCATransitionFromTop;
  98. [imageContainView.layer addAnimation:transition forKey:@"transition"];
  99. }
  100. - (IBAction)button7_click:(id)sender {
  101. [self startAnimation:kCATransitionFade];
  102. }
  103. - (IBAction)button8_click:(id)sender {
  104. [self startAnimation:kCATransitionMoveIn];
  105. }
  106. - (IBAction)button9_click:(id)sender {
  107. [self startAnimation:kCATransitionPush];
  108. }
  109. - (IBAction)button10_click:(id)sender {
  110. [self startAnimation:kCATransitionReveal];
  111. }
  112. - (IBAction)button11_click:(id)sender {
  113. [self startAnimation:@"pageCurl"];
  114. }
  115. - (IBAction)button12_click:(id)sender {
  116. [self startAnimation:@"pageUnCurl"];
  117. }
  118. - (IBAction)button13_click:(id)sender {
  119. [self startAnimation:@"rippleEffect"];
  120. }
  121. - (IBAction)button14_click:(id)sender {
  122. [self startAnimation:@"suckEffect"];
  123. }
  124. - (IBAction)button15_click:(id)sender {
  125. [self startAnimation:@"cube"];
  126. }
  127. - (IBAction)button16_click:(id)sender {
  128. [self startAnimation:@"olgFilp"];
  129. }
  130. - (void)animationWillStart:(CAAnimation *)anim {
  131. NSLog(@"animationWillStart");
  132. }
  133. -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
  134. NSLog(@"animationDidStop");
  135. }

项目结构如下


运行截图



可以看到,fade和oglFilp是无效了,另外cameraIrisHollowOpen和cameraIrisHollowClose也是无效的。

还有当view旋转一定的角度之后,翻页动画也是有问题的。


文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/chy555chy/article/details/51648298

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。