iphonelayer动画_eqi ios layer旋转动画

分类:iphone2012-02-0313:53192人阅读评论(0)收藏举报

原文:

http://nachbaur.com/blog/core-animation-part-1

sample下载地址

http://nachbaur.com/wp-content/uploads/2011/01/CALayerAnimTest.zip

共描述了4个动画

1)轨道环绕,比如月球绕地球

源码:

[html]viewplaincopy
  1. self.view.backgroundColor=[UIColorblackColor];
  2. //Orbit#1
  3. CALayer*orbit1=[CALayerlayer];
  4. orbit1.bounds=CGRectMake(0,0,200,200);
  5. orbit1.position=self.view.center;
  6. orbit1.cornerRadius=100;
  7. orbit1.borderColor=[UIColorredColor].CGColor;
  8. orbit1.borderWidth=1.5;
  9. CALayer*planet1=[CALayerlayer];
  10. planet1.bounds=CGRectMake(0,0,20,20);
  11. planet1.position=CGPointMake(100,0);
  12. planet1.cornerRadius=10;
  13. planet1.backgroundColor=[UIColorredColor].CGColor;
  14. [orbit1addSublayer:planet1];
  15. CABasicAnimation*anim1=[CABasicAnimationanimationWithKeyPath:@"transform.rotation"];
  16. anim1.timingFunction=[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionLinear];
  17. anim1.fromValue=[NSNumbernumberWithFloat:0];
  18. anim1.toValue=[NSNumbernumberWithFloat:((360*M_PI)/180)];
  19. anim1.repeatCount=HUGE_VALF;
  20. anim1.duration=8.0;
  21. [orbit1addAnimation:anim1forKey:@"transform"];
  22. [self.view.layeraddSublayer:orbit1];
  23. //Orbit#2
  24. CALayer*orbit2=[CALayerlayer];
  25. orbit2.bounds=CGRectMake(0,0,120,120);
  26. orbit2.position=planet1.position;
  27. orbit2.cornerRadius=60;
  28. orbit2.borderColor=[UIColorblueColor].CGColor;
  29. orbit2.borderWidth=1.5;
  30. CALayer*planet2=[CALayerlayer];
  31. planet2.bounds=CGRectMake(0,0,16,16);
  32. planet2.position=CGPointMake(60,0);
  33. planet2.cornerRadius=8;
  34. planet2.backgroundColor=[UIColorblueColor].CGColor;
  35. [orbit2addSublayer:planet2];
  36. CABasicAnimation*anim2=[CABasicAnimationanimationWithKeyPath:@"transform.rotation"];
  37. anim2.timingFunction=[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionLinear];
  38. anim2.fromValue=[NSNumbernumberWithFloat:0];
  39. anim2.toValue=[NSNumbernumberWithFloat:((360*M_PI)/180)];
  40. anim2.repeatCount=HUGE_VALF;
  41. anim2.duration=4.0;
  42. [orbit2addAnimation:anim2forKey:@"transform"];
  43. [orbit1addSublayer:orbit2];
  44. //Orbit#3
  45. CALayer*orbit3=[CALayerlayer];
  46. orbit3.bounds=CGRectMake(0,0,72,72);
  47. orbit3.position=planet2.position;
  48. orbit3.cornerRadius=36;
  49. orbit3.borderColor=[UIColorgrayColor].CGColor;
  50. orbit3.borderWidth=1.5;
  51. CALayer*planet3=[CALayerlayer];
  52. planet3.bounds=CGRectMake(0,0,12,12);
  53. planet3.position=CGPointMake(36,0);
  54. planet3.cornerRadius=6;
  55. planet3.backgroundColor=[UIColorgrayColor].CGColor;
  56. [orbit3addSublayer:planet3];
  57. CABasicAnimation*anim3=[CABasicAnimationanimationWithKeyPath:@"transform.rotation"];
  58. anim3.timingFunction=[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionLinear];
  59. anim3.fromValue=[NSNumbernumberWithFloat:0];
  60. anim3.toValue=[NSNumbernumberWithFloat:((360*M_PI)/180)];
  61. anim3.repeatCount=HUGE_VALF;
  62. anim3.duration=2.0;
  63. [orbit3addAnimation:anim3forKey:@"transform"];
  64. [orbit2addSublayer:orbit3];

2。飘动的云

源码

[html]viewplaincopy
  1. UIImage*cloudImage=[UIImageimageNamed:@"cloud.png"];
  2. CALayer*cloud=[CALayerlayer];
  3. cloud.contents=(id)cloudImage.CGImage;
  4. cloud.bounds=CGRectMake(0,0,cloudImage.size.width,cloudImage.size.height);
  5. cloud.position=CGPointMake(self.view.bounds.size.width/2,
  6. cloudImage.size.height/2);
  7. [self.view.layeraddSublayer:cloud];
  8. CGPointstartPt=CGPointMake(self.view.bounds.size.width+cloud.bounds.size.width/2,
  9. cloud.position.y);
  10. CGPointendPt=CGPointMake(cloud.bounds.size.width/-2,
  11. cloud.position.y);
  12. CABasicAnimation*anim=[CABasicAnimationanimationWithKeyPath:@"position"];
  13. anim.timingFunction=[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionLinear];
  14. anim.fromValue=[NSValuevalueWithCGPoint:startPt];
  15. anim.toValue=[NSValuevalueWithCGPoint:endPt];
  16. anim.repeatCount=HUGE_VALF;
  17. anim.duration=8.0;
  18. [cloudaddAnimation:animforKey:@"position"];

iphonelayer动画_eqi ios layer旋转动画
3。点击后放大又缩小的button, 以及点击后飘动的东西,比如删除时,将文件飘到垃圾箱

源码

[html]viewplaincopy
  1. -(void)loadView{
  2. [superloadView];
  3. self.view.backgroundColor=[UIColorwhiteColor];
  4. UIScrollView*scrollView=[[[UIScrollViewalloc]initWithFrame:self.view.bounds]autorelease];
  5. scrollView.bounces=YES;
  6. [self.viewaddSubview:scrollView];
  7. //Scaleoutonclick
  8. {
  9. UILabel*label=[[[UILabelalloc]initWithFrame:CGRectMake(10,10,320,24)]autorelease];
  10. label.text=@"Scalebutton";
  11. label.textAlignment=UITextAlignmentCenter;
  12. [labelsizeToFit];
  13. [scrollViewaddSubview:label];
  14. UIButton*button=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
  15. button.center=CGPointMake(220,30);
  16. button.tag=ButtonActionsBehaviorTypeExpand;
  17. [buttonsetTitle:@"Delete"forState:UIControlStateNormal];
  18. [buttonsizeToFit];
  19. [buttonaddTarget:selfaction:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];
  20. [scrollViewaddSubview:button];
  21. }
  22. //Animatedtrashdelete
  23. {
  24. UILabel*label=[[[UILabelalloc]initWithFrame:CGRectMake(10,126,320,24)]autorelease];
  25. label.text=@"Animateimageintotrashbutton";
  26. label.textAlignment=UITextAlignmentCenter;
  27. [labelsizeToFit];
  28. [scrollViewaddSubview:label];
  29. UIImageView*icon=[[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"carmodel.png"]]autorelease];
  30. icon.center=CGPointMake(290,150);
  31. icon.tag=ButtonActionsBehaviorTypeAnimateTrash;
  32. [scrollViewaddSubview:icon];
  33. UIButton*button=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];
  34. button.center=CGPointMake(40,200);
  35. button.tag=ButtonActionsBehaviorTypeAnimateTrash;
  36. [buttonsetTitle:@"DeleteIcon"forState:UIControlStateNormal];
  37. [buttonsizeToFit];
  38. [buttonaddTarget:selfaction:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];
  39. [scrollViewaddSubview:button];
  40. [scrollViewbringSubviewToFront:icon];
  41. }
  42. //Determinethesizeofallsubviewsofthescrollview
  43. CGRectcontentSize=CGRectZero;
  44. for(UIView*subviewinscrollView.subviews){
  45. contentSize=CGRectUnion(contentSize,subview.frame);
  46. }
  47. scrollView.contentSize=contentSize.size;
  48. }
  49. -(void)buttonClicked:(id)sender{
  50. UIView*senderView=(UIView*)sender;
  51. if(![senderViewisKindOfClass:[UIViewclass]])
  52. return;
  53. switch(senderView.tag){
  54. caseButtonActionsBehaviorTypeExpand:{
  55. CABasicAnimation*anim=[CABasicAnimationanimationWithKeyPath:@"transform"];
  56. anim.timingFunction=[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  57. anim.duration=0.125;
  58. anim.repeatCount=1;
  59. anim.autoreverses=YES;
  60. anim.removedOnCompletion=YES;
  61. anim.toValue=[NSValuevalueWithCATransform3D:CATransform3DMakeScale(1.2,1.2,1.0)];
  62. [senderView.layeraddAnimation:animforKey:nil];
  63. break;
  64. }
  65. caseButtonActionsBehaviorTypeAnimateTrash:{
  66. UIView*icon=nil;
  67. for(UIView*theviewinsenderView.superview.subviews){
  68. if(theview.tag!=ButtonActionsBehaviorTypeAnimateTrash)
  69. continue;
  70. if([theviewisKindOfClass:[UIImageViewclass]]){
  71. icon=theview;
  72. break;
  73. }
  74. }
  75. if(!icon)
  76. return;
  77. UIBezierPath*movePath=[UIBezierPathbezierPath];
  78. [movePathmoveToPoint:icon.center];
  79. [movePathaddQuadCurveToPoint:senderView.center
  80. controlPoint:CGPointMake(senderView.center.x,icon.center.y)];
  81. CAKeyframeAnimation*moveAnim=[CAKeyframeAnimationanimationWithKeyPath:@"position"];
  82. moveAnim.path=movePath.CGPath;
  83. moveAnim.removedOnCompletion=YES;
  84. CABasicAnimation*scaleAnim=[CABasicAnimationanimationWithKeyPath:@"transform"];
  85. scaleAnim.fromValue=[NSValuevalueWithCATransform3D:CATransform3DIdentity];
  86. scaleAnim.toValue=[NSValuevalueWithCATransform3D:CATransform3DMakeScale(0.1,0.1,1.0)];
  87. scaleAnim.removedOnCompletion=YES;
  88. CABasicAnimation*opacityAnim=[CABasicAnimationanimationWithKeyPath:@"alpha"];
  89. opacityAnim.fromValue=[NSNumbernumberWithFloat:1.0];
  90. opacityAnim.toValue=[NSNumbernumberWithFloat:0.1];
  91. opacityAnim.removedOnCompletion=YES;
  92. CAAnimationGroup*animGroup=[CAAnimationGroupanimation];
  93. animGroup.animations=[NSArrayarrayWithObjects:moveAnim,scaleAnim,opacityAnim,nil];
  94. animGroup.duration=0.5;
  95. [icon.layeraddAnimation:animGroupforKey:nil];
  96. break;
  97. }
  98. }
  99. }

4。汽车沿着跑道跑

源码:

[html]viewplaincopy
  1. #definedegreesToRadians(x)(M_PI*x/180.0)
  2. #defineP(x,y)CGPointMake(x,y)
  3. @implementationRaceTrackViewController
  4. -(void)loadView{
  5. [superloadView];
  6. self.view.backgroundColor=[UIColorgreenColor];
  7. UIBezierPath*trackPath=[UIBezierPathbezierPath];
  8. [trackPathmoveToPoint:P(160,25)];
  9. [trackPathaddCurveToPoint:P(300,120)
  10. controlPoint1:P(320,0)
  11. controlPoint2:P(300,80)];
  12. [trackPathaddCurveToPoint:P(80,380)
  13. controlPoint1:P(300,200)
  14. controlPoint2:P(200,480)];
  15. [trackPathaddCurveToPoint:P(140,300)
  16. controlPoint1:P(0,300)
  17. controlPoint2:P(200,220)];
  18. [trackPathaddCurveToPoint:P(210,200)
  19. controlPoint1:P(30,420)
  20. controlPoint2:P(280,300)];
  21. [trackPathaddCurveToPoint:P(70,110)
  22. controlPoint1:P(110,80)
  23. controlPoint2:P(110,80)];
  24. [trackPathaddCurveToPoint:P(160,25)
  25. controlPoint1:P(0,160)
  26. controlPoint2:P(0,40)];
  27. CAShapeLayer*racetrack=[CAShapeLayerlayer];
  28. racetrack.path=trackPath.CGPath;
  29. racetrack.strokeColor=[UIColorblackColor].CGColor;
  30. racetrack.fillColor=[UIColorclearColor].CGColor;
  31. racetrack.lineWidth=30.0;
  32. [self.view.layeraddSublayer:racetrack];
  33. CAShapeLayer*centerline=[CAShapeLayerlayer];
  34. centerline.path=trackPath.CGPath;
  35. centerline.strokeColor=[UIColorwhiteColor].CGColor;
  36. centerline.fillColor=[UIColorclearColor].CGColor;
  37. centerline.lineWidth=2.0;
  38. centerline.lineDashPattern=[NSArrayarrayWithObjects:[NSNumbernumberWithInt:6],[NSNumbernumberWithInt:2],nil];
  39. [self.view.layeraddSublayer:centerline];
  40. CALayer*car=[CALayerlayer];
  41. car.bounds=CGRectMake(0,0,44.0,20.0);
  42. car.position=P(160,25);
  43. car.contents=(id)([UIImageimageNamed:@"carmodel.png"].CGImage);
  44. [self.view.layeraddSublayer:car];
  45. CAKeyframeAnimation*anim=[CAKeyframeAnimationanimationWithKeyPath:@"position"];
  46. anim.path=trackPath.CGPath;
  47. anim.rotationMode=kCAAnimationRotateAuto;
  48. anim.repeatCount=HUGE_VALF;
  49. anim.duration=8.0;
  50. [caraddAnimation:animforKey:@"race"];
  51. }

  

爱华网本文地址 » http://www.aihuau.com/a/25101011/64399.html

更多阅读

Flash鼠绘教程新年动画贺卡 中秋贺卡flash动画

一、素材准备前景准备这个动画的动画很简单,设计工作也不复杂,只需要大家稍稍有些创意就能做好。需要的前景素材包括:对联、门(图1)、小鸡(图2),背景素材包括:背景图(图3)、福字(图4)、春字和音乐。图1图2图3图41.对联

日本历代机器人动画 20世纪篇 机器人动画

来源: 王博雅的日志1963 鉄人28号横山光辉原作TCJ动画中心制作值得纪念的巨大机器人先祖。值得纪念的正太始祖。黑白作品。少年侦探金田正太郎操纵日军在2战中开发的巨大机器人”铁人28号”而恶势力斗争。铁人28号本身没有自己的意志

声明:《iphonelayer动画_eqi ios layer旋转动画》为网友晨光苏醒分享!如侵犯到您的合法权益请联系我们删除