您正在創建一個CAShapeLayer
,但後來沒有做任何有用的事情。我們來解決這個問題。
請勿在-drawRect:
中設置圖層和動畫,因爲這嚴格意味着需要使用CoreGraphics或UIKit API進行繪製。相反,您希望CAShapeLayer繪製三角形 - 這樣您就可以對其進行動畫處理。
CAKeyframeAnimation.path是指完全不同的東西(例如沿路徑移動圖層)。
您的動畫是動畫position
圖層的值。毫不奇怪,它會移動圖層!您想要改爲創建path
值。
CAKeyframeAnimation背後的想法是,您提供了一個values
的數組來設置圖層的屬性。在關鍵幀之間的時間內,它將在兩個相鄰關鍵幀之間進行插值。所以你需要給它幾條路 - 每一條路。
內插任意路徑很困難。當路徑具有相同數量和類型的元素時,CA的路徑插值效果最佳。所以,我們確保我們所有的路徑具有相同的結構,只是有一些點在彼此之上。
動畫的祕密,也許一般電腦:你必須準確解釋你想要發生的事情。 「我想動畫每個點的繪圖,所以它看起來是動畫」是不夠的信息。
這裏有一個UIView子類,我覺得確實你要求什麼,或至少接近。要動畫,請掛鉤按鈕,直到-animate:
動作。
SPAnimatedShapeView.h:
#import <UIKit/UIKit.h>
@interface SPAnimatedShapeView : UIView
- (IBAction)animate:(id)sender;
@end
SPAnimatedShapeView.m:
#import "SPAnimatedShapeView.h"
#import <QuartzCore/QuartzCore.h>
@interface SPAnimatedShapeView()
@property (nonatomic, retain) CAShapeLayer* shapeLayer;
@end
@implementation SPAnimatedShapeView
@synthesize shapeLayer = _shapeLayer;
- (void)dealloc
{
[_shapeLayer release];
[super dealloc];
}
- (void)layoutSubviews
{
if (!self.shapeLayer)
{
self.shapeLayer = [[[CAShapeLayer alloc] init] autorelease];
self.shapeLayer.bounds = CGRectMake(0, 0, 100, 100); // layer is 100x100 in size
self.shapeLayer.position = self.center; // and is centered in the view
self.shapeLayer.strokeColor = [UIColor blueColor].CGColor;
self.shapeLayer.fillColor = [UIColor redColor].CGColor;
self.shapeLayer.lineWidth = 3.f;
[self.layer addSublayer:self.shapeLayer];
}
}
- (IBAction)animate:(id)sender
{
UIBezierPath* path0 = [UIBezierPath bezierPath];
[path0 moveToPoint:CGPointZero];
[path0 addLineToPoint:CGPointZero];
[path0 addLineToPoint:CGPointZero];
[path0 addLineToPoint:CGPointZero];
UIBezierPath* path1 = [UIBezierPath bezierPath];
[path1 moveToPoint:CGPointZero];
[path1 addLineToPoint:CGPointMake(50,100)];
[path1 addLineToPoint:CGPointMake(50,100)];
[path1 addLineToPoint:CGPointMake(50,100)];
UIBezierPath* path2 = [UIBezierPath bezierPath];
[path2 moveToPoint:CGPointZero];
[path2 addLineToPoint:CGPointMake(50,100)];
[path2 addLineToPoint:CGPointMake(100,0)];
[path2 addLineToPoint:CGPointMake(100,0)];
UIBezierPath* path3 = [UIBezierPath bezierPath];
[path3 moveToPoint:CGPointZero];
[path3 addLineToPoint:CGPointMake(50,100)];
[path3 addLineToPoint:CGPointMake(100,0)];
[path3 addLineToPoint:CGPointZero];
CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"path"];
animation.duration = 4.0f;
animation.values = [NSArray arrayWithObjects:(id)path0.CGPath, (id)path1.CGPath, (id)path2.CGPath, (id)path3.CGPath, nil];
[self.shapeLayer addAnimation:animation forKey:nil];
}
@end
解釋正是 「動畫的UIBezierPath繪圖」 的意思。你希望它能夠一個接一個地繪製各個細分市場嗎? – 2012-03-18 21:31:23
@kurt是的,我想動畫每個點的繪製動畫,所以三角形似乎是動畫 – 2012-03-18 23:31:26