經(jīng)常寫動畫,好的動畫效果,可以使應(yīng)用的level提升好幾個檔次,
下面有一個動畫顯得很舒服!個人表示很喜歡,在此做個記錄。
貝塞爾曲線動畫
//貝塞爾曲線動畫
- (IBAction)clickAction:(UIButton *)sender {
//圖片的位置、大小(x,y,w,h)
float imageX = 40;
float imageY = 310;
float imageW = 20;
float imageH = 20;
//把圖片加到view上
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];
imageView.image = [UIImage imageNamed:@"cm_center_discount"];
[self.view addSubview:imageView];
CALayer *layer = [[CALayer alloc] init];
layer.contents = imageView.layer.contents;
layer.frame = imageView.frame;
layer.opacity = 1;
[self.view.layer addSublayer:layer];
//都以self.view為參考系
UIBezierPath *path = [UIBezierPath bezierPath];
//動畫起點
CGPoint startPoint = CGPointMake(imageX+imageW/2, imageY+imageH/2);
[path moveToPoint:startPoint];
//動畫終點
CGPoint endpoint = CGPointMake(320, 568);
//貝塞爾曲線中間點
float sx = startPoint.x;
float sy = startPoint.y;
float ex = endpoint.x;
float ey = endpoint.y;
float x = sx+(ex-sx)/3;
float y = sy+(ey-sy)*0.5-400;
CGPoint centerPoint = CGPointMake(x,y);
[path addQuadCurveToPoint:endpoint controlPoint:centerPoint];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.path = path.CGPath;
animation.fillMode = kCAFillModeForwards;
animation.duration = 0.7;
animation.delegate = self;
animation.autoreverses = YES;//是否自動退回
animation.removedOnCompletion = NO;//完成后是否移除
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[layer addAnimation:animation forKey:@"buy"];
}