AppDelegate.m
Myview *myView = [[Myview alloc] initWithFrame:self.window.bounds];
[self.window addSubview:myView];
[myView release];
MyView.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
moveView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
moveView.backgroundColor = [UIColor redColor];
[self addSubview:moveView];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
CGContextSetLineWidth(ctx, 1);
CGContextSetFillColorWithColor(ctx, [UIColor orangeColor].CGColor);
CGContextMoveToPoint(ctx, 50, 50);
CGContextAddRect(ctx, CGRectMake(50, 50, 150, 50));
CGContextDrawPath(ctx, kCGPathFillStroke);
//CAKeyframeAnimation
//创建一个路径,创建path对象
CGMutablePathRef path = CGPathCreateMutable();
//添加路径
CGPathAddRect(path, nil, CGRectMake(50, 50, 150, 50));
//创建animation对象
//animationWithKeyPath的参数不能随便写,必须是CALayer对象里面的某一个属性
/*
animationWithKeyPath的值:
opacity
margin
zPosition
backgroundColor
cornerRadius
borderWidth
bounds
contents
contentsRect
cornerRadius
frame
hidden
mask
masksToBounds
opacity
position
shadowColor
shadowOffset
shadowOpacity
shadowRadius
*/
CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
keyAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
keyAnimation.duration = 3;
keyAnimation.repeatCount = 1000;
keyAnimation.path = path;
[moveView.layer addAnimation:keyAnimation forKey:@"test"];
//释放
CGPathRelease(path);
}