動(dòng)畫管理類Animal
/**
?*? 開始動(dòng)畫
?*
?*? @param?view? ? ? ? 添加動(dòng)畫的view
? ? ? ? view為button時(shí)? ? _layer.contents = (__bridge id)view.imageView.image.CGImage
?? ? ? ? view 為imageView時(shí)? ? _layer.contents = view.layer.contents
?*? @param?rect? ? ? ? view 的絕對frame
?*? @param?finishPoint 動(dòng)畫終點(diǎn)位置
?*? @param?animationFinisnBlock 動(dòng)畫完成回調(diào)
?*/
-(void)startAnimationandView:(UIButton*)view andRect:(CGRect)rect andFinisnRect:(CGPoint)finishPoint andFinishBlock:(animationFinisnBlock)completion{
?//圖層
? ? _layer= [CALayerlayer];
? ? _layer.contents = view.layer.contents;
? ? _layer.contentsGravity = kCAGravityResizeAspect;
? ? // 改變做動(dòng)畫圖片的大小
? ? rect.size.width=40;
? ? rect.size.height=40;? //重置圖層尺寸
? ? _layer.bounds= rect;
? ? _layer.cornerRadius = rect.size.width/2;
? ? _layer.masksToBounds=YES;? ? ? ? ? //圓角
? ? AppDelegate*delegate = (AppDelegate*)[UIApplicationsharedApplication].delegate;
? ? [delegate.window.layeraddSublayer:_layer];
? ? _layer.position=CGPointMake(rect.origin.x+view.frame.size.width/2,CGRectGetMidY(rect));//開始點(diǎn)
? ? // 路徑
? ? UIBezierPath *path = [UIBezierPath bezierPath];
? ? [pathmoveToPoint:_layer.position];
? ? //確定拋物線的最高點(diǎn)位置? controlPoint
? ? [pathaddQuadCurveToPoint:finishPoint controlPoint:CGPointMake(ScreenWidth/2+100 , rect.origin.y-80)];
? ? //關(guān)鍵幀動(dòng)畫
? ? CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
? ? pathAnimation.path= path.CGPath;
? ? // pathAnimation.delegate = self;
? ? //往下拋時(shí)旋轉(zhuǎn)小動(dòng)畫
? ? CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
? ? rotateAnimation.removedOnCompletion=YES;
? ? rotateAnimation.fromValue= [NSNumbernumberWithFloat:0];
? ? rotateAnimation.toValue= [NSNumbernumberWithFloat:12];
? ? /**
?? ? *? kCAMediaTimingFunctionLinear? 動(dòng)畫從頭到尾的速度是相同的
?? ? kCAMediaTimingFunctionEaseIn? 動(dòng)畫以低速開始瓶竭。
?? ? kCAMediaTimingFunctionEaseOut? 動(dòng)畫以低速結(jié)束荤堪。
?? ? kCAMediaTimingFunctionEaseInEaseOut? 動(dòng)畫以低速開始和結(jié)束沟绪。
?? ? kCAMediaTimingFunctionDefault
?? ? */
? ? rotateAnimation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
? ? CAAnimationGroup *groups = [CAAnimationGroup animation];
? ? groups.animations=@[pathAnimation,rotateAnimation];
? ? groups.duration=1.2f;
? ? //設(shè)置之后做動(dòng)畫的layer不會(huì)回到一開始的位置
? ? groups.removedOnCompletion=NO;
? ? groups.fillMode=kCAFillModeForwards;
? ? //讓工具類成為組動(dòng)畫的代理
? ? groups.delegate=self;
? ? [_layer addAnimation:groups forKey:@"1"];
? ? if(completion) {
? ? ? ? _animationFinisnBlock= completion;
? ? }
}
//動(dòng)畫完成后代理
- (void)animationDidStop:(CAAnimation*)anim finished:(BOOL)flag
{
? ? //? ? [anim def];
? ? if(anim == [_layeranimationForKey:@"1"]) {
? ? ? ? [_layer removeFromSuperlayer];
? ? ? ? _layer=nil;
? ? ? ? if (_animationFinisnBlock) {
? ? ? ? ? ? _animationFinisnBlock(YES);
? ? ? ? }
? ? }
}
//上下抖動(dòng)動(dòng)畫
+(void)shakeAnimation:(UIView*)shakeView
{
? ? CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
? ? shakeAnimation.duration=0.25f;
? ? shakeAnimation.fromValue= [NSNumbernumberWithFloat:-5];
? ? shakeAnimation.toValue= [NSNumbernumberWithFloat:5];
? ? shakeAnimation.autoreverses=YES;
? ? [shakeView.layeraddAnimation:shakeAnimationforKey:nil];
}