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

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條評論
主站蜘蛛池模板: 国产精品永久免费自在线观看 | 欧美天天干 | 男人的天堂视频在线 | 免费视频精品一区二区三区 | 天天做天天爱夜夜爽女人爽宅 | 国产视频自拍一区 | 伊人激情网 | 九九99re在线视频精品免费 | 午夜一级精品免费毛片 | 2020久久精品国产免费 | 一级大片视频 | 精品久久久久久亚洲 | 激情五月综合综合久久69 | 成人激情视频 | 欧美成人三级一区二区在线观看 | 九九亚洲视频 | 午夜国产福利在线 | 亚洲综合无码一区二区 | 久久久久久免费视频 | 黄色影院免费观看 | 久艹在线播放 | 久久做| 午夜免费福利影院 | 伊人久久在线视频 | 男人天堂网在线视频 | 国产97在线观看 | 视频一区精品 | 亚洲国产欧美在线不卡中文 | 欧美黑人巨大3dvideo | 天天操夜夜 | 91福利在线免费观看 | 亚洲va欧美va国产va天堂 | 成人在线免费视频播放 | 色综合色综合色综合 | 青青热在线观看视频精品 | 亚洲最大色网站 | 久草在线观看资源 | 久久精品久久久 | 老司机午夜影院 | 成年男女免费视频网站 | 69色视频日韩在线视频 |