亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

iPhone: 對象沿路徑動畫

系統 1776 0

這里我將展示如何在UIView上讓對象沿著路徑動畫,我將創建路徑并畫到UIView上讓你能都看見,并沿相同的路徑來做動畫。

我在添加到屏幕的UIView完成所有的這些…

首先,我們在屏幕上畫一條曲線

    
  1. //Thisdrawsaquadraticbeziercurvedlinerightacrossthescreen
  2. -( void )drawACurvedLine{
  3. //Createabitmapgraphicscontext,youwilllatergetaUIImagefromthis
  4. UIGraphicsBeginImageContext(CGSizeMake(320,460));
  5. CGContextRefctx=UIGraphicsGetCurrentContext();
  6. //Setvariablesinthecontextfordrawing
  7. CGContextSetLineWidth(ctx,1.5);
  8. CGContextSetStrokeColorWithColor(ctx,[UIColorwhiteColor].CGColor);
  9. //Setthestartpointofyourdrawing
  10. CGContextMoveToPoint(ctx,10,10);
  11. //Theendpointofthelineis310,450....i'malsosettingareferencepointof10,450
  12. //Aquadraticbeziercurveisdrawnusingthesecoordinates-experimentandseetheresults.
  13. CGContextAddQuadCurveToPoint(ctx,10,450,310,450);
  14. //Addanothercurve,theoppositeoftheabove-finishingbackwherewestarted
  15. CGContextAddQuadCurveToPoint(ctx,310,10,10,10);
  16. //Drawtheline
  17. CGContextDrawPath(ctx,kCGPathStroke);
  18. //GetaUIImagefromthecurrentbitmapcontextwecreatedatthestartandthenendtheimagecontext
  19. UIImage*curve=UIGraphicsGetImageFromCurrentImageContext();
  20. UIGraphicsEndImageContext();
  21. //Withtheimage,weneedaUIImageView
  22. UIImageView*curveView=[[UIImageViewalloc]initWithImage:curve];
  23. //Settheframeoftheview-whichisusedtopositionitwhenweaddittoourcurrentUIView
  24. curveView.frame=CGRectMake(1,1,320,460);
  25. curveView.backgroundColor=[UIColorclearColor];
  26. [selfaddSubview:curveView];
  27. }

現在我我創建了一個關鍵幀動畫,并添加一個和我們話得線一樣的路徑。我們還將畫一個圈,并沿著路徑做動畫:

    
  1. -( void )animateCicleAlongPath{
  2. //Preparetheanimation-weusekeyframeanimationforanimationsofthiscomplexity
  3. CAKeyframeAnimation*pathAnimation=[CAKeyframeAnimationanimationWithKeyPath:@ "position" ];
  4. //Setsomevariablesontheanimation
  5. pathAnimation.calculationMode=kCAAnimationPaced;
  6. //Wewanttheanimationtopersist-notsoimportantinthiscase-butkeptforclarity
  7. //Ifweanimatedsomethingfromlefttoright-andwewantedittostayinthenewposition,
  8. //thenwewouldneedtheseparameters
  9. pathAnimation.fillMode=kCAFillModeForwards;
  10. pathAnimation.removedOnCompletion=NO;
  11. pathAnimation.duration=5.0;
  12. //Letsloopcontinuouslyforthedemonstration
  13. pathAnimation.repeatCount=1000;
  14. //Setupthepathfortheanimation-thisisverysimilarasthecodethedrawtheline
  15. //insteadofdrawingtothegraphicscontext,insteadwedrawlinesonaCGPathRef
  16. CGPointendPoint=CGPointMake(310,450);
  17. CGMutablePathRefcurvedPath=CGPathCreateMutable();
  18. CGPathMoveToPoint(curvedPath,NULL,10,10);
  19. CGPathAddQuadCurveToPoint(curvedPath,NULL,10,450,310,450);
  20. CGPathAddQuadCurveToPoint(curvedPath,NULL,310,10,10,10);
  21. //Nowwehavethepath,wetelltheanimationwewanttousethispath-thenwereleasethepath
  22. pathAnimation.path=curvedPath;
  23. CGPathRelease(curvedPath);
  24. //Wewillnowdrawacircleatthestartofthepathwhichwewillanimatetofollowthepath
  25. //Weusethesametechniqueasbeforetodrawtoabitmapcontextandtheneventuallycreate
  26. //aUIImageViewwhichweaddtoourview
  27. UIGraphicsBeginImageContext(CGSizeMake(20,20));
  28. CGContextRefctx=UIGraphicsGetCurrentContext();
  29. //Setcontextvariables
  30. CGContextSetLineWidth(ctx,1.5);
  31. CGContextSetFillColorWithColor(ctx,[UIColorgreenColor].CGColor);
  32. CGContextSetStrokeColorWithColor(ctx,[UIColorwhiteColor].CGColor);
  33. //Drawacircle-andpaintitwithadifferentoutline(white)andfillcolor(green)
  34. CGContextAddEllipseInRect(ctx,CGRectMake(1,1,18,18));
  35. CGContextDrawPath(ctx,kCGPathFillStroke);
  36. UIImage*circle=UIGraphicsGetImageFromCurrentImageContext();
  37. UIGraphicsEndImageContext();
  38. UIImageView*circleView=[[UIImageViewalloc]initWithImage:circle];
  39. circleView.frame=CGRectMake(1,1,20,20);
  40. [selfaddSubview:circleView];
  41. //AddtheanimationtothecircleView-onceyouaddtheanimationtothelayer,theanimationstarts
  42. [circleView.layeraddAnimation:pathAnimationforKey:@ "moveTheSquare" ];
  43. }

要讓所有的都跑起來,你可以使用init函數:

    
  1. -(id)initWithFrame:(CGRect)frame{
  2. if (self=[superinitWithFrame:frame]){
  3. [selfdrawACurvedLine];
  4. [selfanimateCicleAlongPath];
  5. }
  6. return self;
  7. }

在你的ViewController中像這樣寫

    
  1. -( void )viewDidLoad{
  2. UIView*customView=[[Canvasalloc]initWithFrame:CGRectMake(0,0,320,460)];
  3. customView.backgroundColor=[UIColorblackColor];
  4. [self.viewaddSubview:customView];
  5. [customViewrelease];
  6. [superviewDidLoad];
  7. }

還有,不要忘記添加 Quartz 引用:

    
  1. #import<QuartzCore/QuartzCore.h>

我確定有很多更好的方式來做這個事,例如使用CALayers 和添加CGImage 到layers。但是那是一些我沒有嘗試的東西。上面的例子應該足夠讓你開始沿著路徑做動畫。

Animate along a path

這里是工程拷貝http://blog.devedup.com/wp-content/uploads/2010/06/AnimateAlongAPath.zip

(譯者水平有限,歡迎指正。)

iPhone: 對象沿路徑動畫


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 九九亚洲综合精品自拍 | 欧美中文字幕在线视频 | 欧美亚洲日本国产 | 亚洲欧美日韩精品香蕉 | 二区三区| 全免费一级午夜毛片 | 九九综合 | 四虎www成人影院 | 国产 magnet| 免费观看黄色的网站 | 一级毛片在线视频 | 精品视频一区二区三区在线播放 | 一级成人| 国产主播在线看 | 97人人干 | 日本成片 | 日韩精品久久久久久 | 热久久99精品这里有精品 | 看黄a大片 免费 | 色综合图区 | 一级特黄女人生活片 | 国产成人综合在线视频 | 欧美国产日韩在线播放 | 亚洲欧洲国产精品你懂的 | 亚洲综合色在线观看 | 久久日本精品99久久久久 | 国产成人综合久久精品红 | 日本高清视频www夜色资源网 | 成熟女人免费一级毛片 | 亚洲视频在线观看免费 | 日本一区二区三区免费看 | 国产一区二区视频在线播放 | 日韩视频一区二区三区 | 91官网 | 手机看片日韩国产一区二区 | 欧美福利在线视频 | 久久免费精品国产视频 | 亚洲一区日韩 | 亚洲欧洲日韩国产一区二区三区 | 女孕学护士一级毛片 | 国产一级毛片国语普通话对白 |