iOS 對象沿著指定路徑進行動畫

對象的動畫運動路徑設

以下方法可作為兩步實現:

以下方法在自定義view的類里面實現,可放在自定義函數中自行控制觸法。也可放在drawRect或layoutSubviews里面讓view在顯 示時觸法。但推薦放在自定義view中觸發,因為這樣才可以做到自行控制,并保證同時只有一個在運行,否則會因為view的改變導致重繪,導致同時執行多 個相同的動畫,會影響效果和耗費內存。

一》讓view對象沿指定的路徑進行動畫的方法:

以下是指定路徑:

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

//Set some variables on the animation

pathAnimation.calculationMode = kCAAnimationPaced;

//We want the animation to persist - not so important in this case - but kept for clarity

//If we animated something from left to right - and we wanted it to stay in the new position,

//then we would need these parameters

pathAnimation.fillMode = kCAFillModeForwards;

pathAnimation.removedOnCompletion = NO;

pathAnimation.duration = 10;

//Lets loop continuously for the demonstration

pathAnimation.repeatCount = 10;

//Setup the path for the animation - this is very similar as the code the draw the line

//instead of drawing to the graphics context, instead we draw lines on a CGPathRef

//CGPoint endPoint = CGPointMake(310, 450);

CGMutablePathRef curvedPath = CGPathCreateMutable();

CGPathMoveToPoint(curvedPath, NULL, 10, 10);

CGPathAddQuadCurveToPoint(curvedPath, NULL, 10, 450, 310, 450);

CGPathAddQuadCurveToPoint(curvedPath, NULL, 310, 10, 10, 10);

//Now we have the path, we tell the animation we want to use this path - then we release the path

pathAnimation.path = curvedPath;

CGPathRelease(curvedPath);

在指定路徑后,指定動畫的對象,(在此用UIImageView舉例:)

UIImageView *circleView = [[UIImageView alloc] initWithImage:circle];

circleView.frame = CGRectMake(1, 1, 40, 40);

[self addSubview:circleView];

//Add the animation to the circleView - once you add the animation to the layer, the animation starts

[circleView.layer addAnimation:pathAnimation

forKey:@"moveTheSquare"];

二>.若要把指定的路徑以線條或其他方法顯示出來,則要繪制路徑,方法是:

UIGraphicsBeginImageContext(CGSizeMake(320,460));

CGContextRef ctx = UIGraphicsGetCurrentContext();

//Set variables in the context for drawing

CGContextSetLineWidth(ctx, 1.5);

CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);

//Set the start point of your drawing

CGContextMoveToPoint(ctx, 10, 10);

//The end point of the line is 310,450 .... i'm also setting a reference point of 10,450

//A quadratic bezier curve is drawn using these coordinates - experiment and see the results.

CGContextAddQuadCurveToPoint(ctx, 10, 450, 310, 450);

//Add another curve, the opposite of the above - finishing back where we started

CGContextAddQuadCurveToPoint(ctx, 310, 10, 10, 10);

//Draw the line

CGContextDrawPath(ctx, kCGPathStroke);

若要繪制圖片背景(直接添加即可),則:

//With the image, we need a UIImageView

UIImage *image = [UIImage imagewithName:@"a.png"];

UIImageView *curveView = [[UIImageView alloc] image];

//Set the frame of the view - which is used to position it when we add it to our current UIView

curveView.frame = CGRectMake(1, 1, 320, 460);

curveView.backgroundColor = [UIColor greenColor];

[self addSubview:curveView];

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 以下方法可作為兩步實現: 以下方法在自定義view的類里面實現,可放在自定義函數中自行控制觸法。也可放在drawR...
    Legendary閱讀 1,091評論 0 1
  • 我有一個背包 我和它一起走了很長的路 我的背包很輕很輕 媽媽說別往里面裝太多東西 在前行的路上 許多的更多的 看著...
    李唐的小詩閱讀 225評論 0 0
  • 七言古絕·頌友三 民不畏死死何懼, 人若貪生生可歡? 拍案而出悲公仆, 一腔熱血斥青天! 注:聞一多字友三;公...
    任爾風云我自逍閱讀 171評論 9 5
  • 每次回老家看著侄女們的幾個孩子在家里為一根火腿腸,一個玩具,爭得不可開交時,就對他們一頓大吼:“不要在家里鬧了,出...
    夏冰日記閱讀 629評論 4 3
  • 我的家鄉在龍舟之鄉——沙田,沙田最美的地方就是濱江路。濱江路是一條美麗的臨江靠漁港的水鄉路。東起港口大道中心...
    語歌晨唱閱讀 1,203評論 0 1