1.使用扩展的方式对UIView添加方法:
- (void)animationStartPoint:(CGPoint)start endPoint:(CGPoint)end didStopAnimation:(void (^)(void))event{
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:start];
//设置控制点
CGPoint point1 = CGPointZero;
CGPoint point2 = CGPointZero;
if (start.x > end.x) {
point1 = CGPointMake((start.x - end.x) / 4 * 3 + end.x, start.y - 40);
point2 = CGPointMake((start.x - end.x) / 4 + end.x, (end.y - start.y) / 2 + start.y);
}else {
point1 = CGPointMake((end.x - start.x) / 4 + start.x , start.y - 40);
point2 = CGPointMake((end.x - start.x) / 4 * 3 + start.x, (end.y - start.y) / 2 + start.y);
}
[path addCurveToPoint:end controlPoint1:point1 controlPoint2:point2];
//路径
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.path = path.CGPath;
animation.rotationMode = kCAAnimationRotateAuto;
//缩放
CABasicAnimation *baAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//控制缩放的倍数,根据项目自定
baAnimation.fromValue = @1;
baAnimation.toValue = @1;
baAnimation.autoreverses = YES;
//动画组合
CAAnimationGroup *groups = [CAAnimationGroup animation];
groups.animations = @[animation,baAnimation];
groups.duration = 0.5;
groups.removedOnCompletion = NO;
groups.fillMode = kCAFillModeForwards;
groups.delegate = self;
[groups setValue:@"groupsAnimation" forKey:@"animationName"];
[self.layer addAnimation:groups forKey:nil];
self.animStop = event;
}
#pragma mark - CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
self.animStop();
}
- (void)setAnimStop:(void (^)(void))animStop {
objc_setAssociatedObject(self, @"animStop", animStop, OBJC_ASSOCIATION_COPY);
}
- (void (^)(void))animStop {
return objc_getAssociatedObject(self, @"animStop");
}
2.使用:
#import "AddToCarViewController.h"
@interface AddToCarViewController ()
@property (nonatomic, strong)UIButton *addBtn;
@property (nonatomic, strong)UIButton *carBtn;
@end
@implementation AddToCarViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.addBtn = [[UIButton alloc]initWithFrame:CGRectMake(UI_SCREEN_WIDTH - 60, UI_SCREEN_HEIGHT / 2, 20, 20)];
self.addBtn.layer.cornerRadius = 10;
self.addBtn.layer.masksToBounds = YES;
[self.addBtn setTitle:@"+" forState:UIControlStateNormal];
[self.addBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.addBtn.backgroundColor = [UIColor purpleColor];
[self.addBtn addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.addBtn];
self.carBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, UI_SCREEN_HEIGHT - HOME_INDICATOR_HEIGHT - 60, 120, 60)];
self.carBtn.backgroundColor = [UIColor purpleColor];
[self.carBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.carBtn setTitle:@"购物车" forState:UIControlStateNormal];
[self.view addSubview:self.carBtn];
}
//添加
- (void)addAction {
UIView *tempView = [[UIView alloc]initWithFrame:self.addBtn.frame];
tempView.backgroundColor = [UIColor redColor];
[self.view addSubview:tempView];
[tempView animationStartPoint:tempView.center endPoint:self.carBtn.center didStopAnimation:^{
[tempView removeFromSuperview];
}];
}