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

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條評論
主站蜘蛛池模板: 国语高清精品一区二区三区 | 国内一级特黄女人精品片 | 国产精品久久国产三级国不卡顿 | 在线精品亚洲欧洲第一页 | 久久国产精品老女人 | 中文字幕日韩一区二区不卡 | 五月婷婷欧美 | 香蕉久久久久 | 亚洲欧美一区二区视频 | 四虎永久免费地址在线网站 | 国产亚洲精品线观看77 | 久久国产精品亚洲 | 色第一页 | 精品国产欧美一区二区三区成人 | 亚洲视频在线一区 | 色优久久 | 欧美一级视频免费看 | 日本草草影院 | 免费一级毛片在线播放泰国 | 夜夜摸视频网 | 99视频国产热精品视频 | 美国一级毛片免费看成人 | 亚洲欧美二区三区久本道 | 国产夜色 | 欧美激情aa毛片 | 中文字幕国产专区 | 欧美一级毛片免费播放aa | 九九热视频免费 | 手机看片福利视频 | 国产精品久久久亚洲 | 三及毛片 | 在线 色 | 2021国产精品自产拍在线 | 一级片手机在线观看 | 尤物视频在线观看视频 | 欧美麻豆久久久久久中文 | 精品伊人久久久99热这里只 | 天天射天天色天天干 | 99j久久精品久久久久久 | 久久久久无码国产精品一区 | 国产亚洲玖玖玖在线观看 |