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

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條評論
主站蜘蛛池模板: 99这里只有精品6 | 91热精品 | 午夜免费福利在线 | 日本一区二区三区欧美在线观看 | 男人天堂免费 | 性综合网| 国产午夜不卡 | 91成品视频 | a毛片在线播放 | 在线 中文字幕 日韩 欧美 | 国产精品自拍一区 | 99精品中文字幕 | 亚洲免费黄色 | 欧美日韩国产亚洲一区二区三区 | 免费一级片 | 中国一级毛片aaa片 中国一级毛片录像 | 每日更新国产精品视频 | 在线免费黄色网址 | 2018一级毛片免费观看 | 国产成人精品一区二区免费 | 一二三区在线观看 | 日本精品久久久中文字幕 | 日韩不卡一区二区三区 | 一级欧美毛片成人免费视频 | 亚洲一区不卡视频 | 美女被羞羞产奶视频网站 | 福利姬在线视频国产观看 | 国产日本欧美亚洲精品视 | 国产欧美精品 | 精品日韩一区二区三区视频 | 久久尹人香蕉国产免费天天 | 欧美特级爽毛片 | 亚洲色无码播放 | 亚洲国产欧美国产综合一区 | 国产精品久久久久久久久 | 男人天堂一区 | 九九在线视频 | 欧美日韩免费做爰视频 | 欧美日韩国产精品综合 | 梦想成为魔法少女在线观看 | 天天做天天爱夜夜爽女人爽宅 |