- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event
{
//图片向下消失
// [self animationRevealFromBottom:self.iconView];
//图片向上消失
// [self animationRevealFromTop:self.iconView];
//图片向右消失
// [self animationRevealFromLeft:self.iconView];
//图片向左消失
// [self animationRevealFromRight:self.iconView];
//图片想上翻页
// [self animationCurlUp:self.iconView];
//图片想下翻页
// [self animationCurlDown:self.iconView];
//图片向上推动
// [self animationPushUp:self.iconView];
//图片向下推动
// [self animationPushDown:self.iconView];
//图片向左推动
// [self animationPushLeft:self.iconView];
//图片向右推动
// [self animationPushRight:self.iconView];
//图片从下往上推出 带有动画时间
// [self animationMoveUp:self.iconView duration:2.0];
//图片从上往下推出 带有动画时间
// [self animationMoveDown:self.iconView duration:2.0];
//图片从左往右推出
// [self animationMoveLeft:self.iconView];
//图片从右往左推出
// [self animationMoveRight:self.iconView];
//图片旋转变小 又变大
// [self animationRotateAndScaleEffects:self.iconView];
//图片旋转变小 旋转变大
// [self animationRotateAndScaleDownUp:self.iconView];
//图片翻转(从上往下)
// [self animationFlipFromTop:self.iconView];
//图片翻转(从下往上)
// [self animationFlipFromBottom:self.iconView];
//立体翻转(从左往右)
// [self animationCubeFromLeft:self.iconView];
//立体翻转(从右往左)
// [self animationCubeFromRight:self.iconView];
//立体翻转(从上往下)
// [self animationCubeFromTop:self.iconView];
//立体翻转(从下往上)
// [self animationCubeFromBottom:self.iconView];
//往左上角飞出
// [self animationSuckEffect:self.iconView];
//抖动
// [self animationRippleEffect:self.iconView];
//相机打开的动画
// [self animationCameraOpen:self.iconView];
//相机关闭的动画
// [self animationCameraClose:self.iconView];
//放大弹跳 移除
// [self dapWithView:self.iconView];
//上下弹跳
// [self animationShootOut:self.iconView];
//上下减速弹跳
shakerAnimation(self.iconView,2,200.0);
}
- (void)animationCameraClose:(UIView *)view
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.35f];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation setType:@"cameraIrisHollowClose"];
[animation setSubtype:@"fromRight"];
[view.layer addAnimation:animation forKey:nil];
}
- (void)animationCameraOpen:(UIView *)view
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.35f];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation setType:@"cameraIrisHollowOpen"];
[animation setSubtype:@"fromRight"];
[view.layer addAnimation:animation forKey:nil];
}
- (void)animationRippleEffect:(UIView *)view
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.35f];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation setType:@"rippleEffect"];
[view.layer addAnimation:animation forKey:nil];
}
- (void)animationSuckEffect:(UIView *)view
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.35f];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation setType:@"suckEffect"];
[view.layer addAnimation:animation forKey:nil];
}
- (void)animationCubeFromTop:(UIView *)view
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.35f];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation setType:@"cube"];
[animation setSubtype:@"fromTop"];
[view.layer addAnimation:animation forKey:nil];
}
- (void)animationCubeFromBottom:(UIView *)view
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.35f];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation setType:@"cube"];
[animation setSubtype:@"fromBottom"];
[view.layer addAnimation:animation forKey:nil];
}
- (void)animationCubeFromRight:(UIView *)view
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.35f];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation setType:@"cube"];
[animation setSubtype:@"fromRight"];
[view.layer addAnimation:animation forKey:nil];
}
- (void)animationCubeFromLeft:(UIView *)view
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.35f];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation setType:@"cube"];
[animation setSubtype:@"fromLeft"];
[view.layer addAnimation:animation forKey:nil];
}
- (void)animationFlipFromBottom:(UIView *)view
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.35f];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation setType:@"oglFlip"];
[animation setSubtype:@"fromBottom"];
[view.layer addAnimation:animation forKey:nil];
}
- (void)animationFlipFromTop:(UIView *)view
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.35f];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation setType:@"oglFlip"];
[animation setSubtype:@"fromTop"];
[view.layer addAnimation:animation forKey:nil];
}
/** CABasicAnimation
*
* @see https://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreAnimation_guide/Articles/KVCAdditions.html
*
* @brief 便利构造函数 animationWithKeyPath: KeyPath需要一个字符串类型的参数,实际上是一个
* 键-值编码协议的扩展,参数必须是CALayer的某一项属性,你的代码会对应的去改变该属性的效果
* 具体可以填写什么请参考上面的URL,切勿乱填!
* 例如这里填写的是 @"transform.rotation.z" 意思就是围绕z轴旋转,旋转的单位是弧度.
* 这个动画的效果是把view旋转到最小,再旋转回来.
* 你也可以填写@"opacity" 去修改透明度...以此类推.修改layer的属性,可以用这个类.
*
* @param toValue 动画结束的值.CABasicAnimation自己只有三个属性(都很重要)(其他属性是继承来的),分别为:
* fromValue(开始值), toValue(结束值), byValue(偏移值),
! 这三个属性最多只能同时设置两个;
* 他们之间的关系如下:
* 如果同时设置了fromValue和toValue,那么动画就会从fromValue过渡到toValue;
* 如果同时设置了fromValue和byValue,那么动画就会从fromValue过渡到fromValue + byValue;
* 如果同时设置了byValue 和toValue,那么动画就会从toValue - byValue过渡到toValue;
*
* 如果只设置了fromValue,那么动画就会从fromValue过渡到当前的value;
* 如果只设置了toValue ,那么动画就会从当前的value过渡到toValue;
* 如果只设置了byValue ,那么动画就会从从当前的value过渡到当前value + byValue.
*
* 可以这么理解,当你设置了三个中的一个或多个,系统就会根据以上规则使用插值算法计算出一个时间差并
* 同时开启一个Timer.Timer的间隔也就是这个时间差,通过这个Timer去不停地刷新keyPath的值.
! 而实际上,keyPath的值(layer的属性)在动画运行这一过程中,是没有任何变化的,它只是调用了GPU去
* 完成这些显示效果而已.
* 在这个动画里,是设置了要旋转到的弧度,根据以上规则,动画将会从它当前的弧度专旋转到我设置的弧度.
*
* @param duration 动画持续时间
*
* @param timingFunction 动画起点和终点之间的插值计算,也就是说它决定了动画运行的节奏,是快还是慢,还是先快后慢...
*/
/** CAAnimationGroup
*
* @brief 顾名思义,这是一个动画组,它允许多个动画组合在一起并行显示.比如这里设置了两个动画,
* 把他们加在动画组里,一起显示.例如你有几个动画,在动画执行的过程中需要同时修改动画的某些属性,
* 这时候就可以使用CAAnimationGroup.
*
* @param duration 动画持续时间,值得一提的是,如果添加到group里的子动画不设置此属性,group里的duration会统一
* 设置动画(包括子动画)的duration属性;但是如果子动画设置了duration属性,那么group的duration属性
* 的值不应该小于每个子动画中duration属性的值,否则会造成子动画显示不全就停止了动画.
*
* @param autoreverses 动画完成后自动重新开始,默认为NO.
*
* @param repeatCount 动画重复次数,默认为0.
*
* @param animations 动画组(数组类型),把需要同时运行的动画加到这个数组里.
*
* @note addAnimation:forKey 这个方法的forKey参数是一个字符串,这个字符串可以随意设置.
*
* @note 如果你需要在动画group执行结束后保存动画效果的话,设置 fillMode 属性,并且把
* removedOnCompletion 设置为NO;
*/
- (void)animationRotateAndScaleDownUp:(UIView *)view
{
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat:(2 * M_PI) * 2];
rotationAnimation.duration = 0.35f;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.toValue = [NSNumber numberWithFloat:0.0];
scaleAnimation.duration = 0.35f;
scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.duration = 0.35f;
animationGroup.autoreverses = YES;
animationGroup.repeatCount = 1;
animationGroup.animations =[NSArray arrayWithObjects:rotationAnimation, scaleAnimation, nil];
[view.layer addAnimation:animationGroup forKey:@"animationGroup"];
}
- (void)animationRotateAndScaleEffects:(UIView *)view
{
[UIView animateWithDuration:0.35f animations:^
{
/**
* @see http://donbe.blog.163.com/blog/static/138048021201061054243442/
*
* @param transform 形变属性(结构体),可以利用这个属性去对view做一些翻转或者缩放.详解请猛戳↑URL.
*
* @method valueWithCATransform3D: 此方法需要一个CATransform3D的结构体.一些非详细的讲解可以看下面的URL
*
* @see http://blog.csdn.net/liubo0_0/article/details/7452166
*
*/
view.transform = CGAffineTransformMakeScale(0.001, 0.001);
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
// 向右旋转45°缩小到最小,然后再从小到大推出.
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.70, 0.40, 0.80)];
/**
* 其他效果:
* 从底部向上收缩一半后弹出
* animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.0, 1.0, 0.0)];
*
* 从底部向上完全收缩后弹出
* animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1.0, 0.0, 0.0)];
*
* 左旋转45°缩小到最小,然后再从小到大推出.
* animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.50, -0.50, 0.50)];
*
* 旋转180°缩小到最小,然后再从小到大推出.
* animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0.1, 0.2, 0.2)];
*/
animation.duration = 0.45;
animation.repeatCount = 1;
[view.layer addAnimation:animation forKey:nil];
}
completion:^(BOOL finished)
{
[UIView animateWithDuration:0.35f animations:^
{
view.transform = CGAffineTransformMakeScale(1.0, 1.0);
}];
}];
}
- (void)animationMoveLeft:(UIView *)view
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.35f];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation setType:kCATransitionMoveIn];
[animation setSubtype:kCATransitionFromLeft];
[view.layer addAnimation:animation forKey:nil];
}
- (void)animationMoveRight:(UIView *)view
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.35f];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation setType:kCATransitionMoveIn];
[animation setSubtype:kCATransitionFromRight];
[view.layer addAnimation:animation forKey:nil];
}
- (void)animationMoveDown:(UIView *)view duration:(CFTimeInterval)duration
{
CATransition *transition = [CATransition animation];
transition.duration =0.4;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionReveal;
transition.subtype = kCATransitionFromBottom;
[view.layer addAnimation:transition forKey:nil];
}
- (void)animationMoveUp:(UIView *)view duration:(CFTimeInterval)duration
{
CATransition *animation = [CATransition animation];
[animation setDuration:duration];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[animation setType:kCATransitionMoveIn];
[animation setSubtype:kCATransitionFromTop];
[view.layer addAnimation:animation forKey:nil];
}
- (void)animationPushLeft:(UIView *)view
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.35f];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromLeft];
[view.layer addAnimation:animation forKey:nil];
}
- (void)animationPushRight:(UIView *)view
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.35f];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[view.layer addAnimation:animation forKey:nil];
}
- (void)animationPushDown:(UIView *)view
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.35f];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromBottom];
[view.layer addAnimation:animation forKey:nil];
}
- (void)animationPushUp:(UIView *)view
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.35f];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromTop];
[view.layer addAnimation:animation forKey:nil];
}
- (void)animationCurlDown:(UIView *)view
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.35f];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:view cache:NO];
[UIView commitAnimations];
}
- (void)animationCurlUp:(UIView *)view
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:0.35f];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:view cache:NO];
[UIView commitAnimations];
}
- (void)animationRevealFromRight:(UIView *)view
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.35f];
[animation setType:kCATransitionReveal];
[animation setSubtype:kCATransitionFromRight];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[view.layer addAnimation:animation forKey:nil];
}
- (void)animationRevealFromLeft:(UIView *)view
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.35f];
[animation setType:kCATransitionReveal];
[animation setSubtype:kCATransitionFromLeft];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[view.layer addAnimation:animation forKey:nil];
}
- (void)animationRevealFromBottom:(UIView *)view
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.35f];
[animation setType:kCATransitionReveal];
[animation setSubtype:kCATransitionFromBottom];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
[view.layer addAnimation:animation forKey:nil];
}
- (void)animationRevealFromTop:(UIView *)view
{
CATransition *animation = [CATransition animation];
[animation setDuration:0.35f];
[animation setType:kCATransitionReveal];
[animation setSubtype:kCATransitionFromTop];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[view.layer addAnimation:animation forKey:nil];
}
- (void)dapWithView:(UIView *)animateImageView
{
[UIView animateWithDuration:0.6f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^(void){
animateImageView.frame = CGRectMake(0, 0, 320, 460);
} completion:^(BOOL finished){
[UIView animateWithDuration:0.2f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(void){
animateImageView.frame = CGRectMake(0, -60, 320, 460); //弹起
} completion:^(BOOL finished){
[UIView animateWithDuration:0.15f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^(void){
animateImageView.frame = CGRectMake(0, 0, 320, 460);
} completion:^(BOOL finished){
[UIView animateWithDuration:0.05f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(void){
animateImageView.frame = CGRectMake(0, -20, 320, 460); //弹起
} completion:^(BOOL finished){
[UIView animateWithDuration:0.1f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^(void){
animateImageView.frame = CGRectMake(0, 0, 320, 460);
} completion:^(BOOL finished){
[UIView animateWithDuration:0.1f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(void){
animateImageView.frame = CGRectMake(0, -4, 320, 460); //弹起
} completion:^(BOOL finished){
[UIView animateWithDuration:0.1f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^(void){
animateImageView.frame = CGRectMake(0, 0, 320, 460);
} completion:^(BOOL finished){
[UIView animateWithDuration:0.3f delay:0.5f options:UIViewAnimationOptionCurveEaseIn animations:^(void){
animateImageView.frame = CGRectMake(0, -460, 320, 460);
} completion:^(BOOL finished){
[animateImageView removeFromSuperview];
[animateImageView.layer removeFromSuperlayer];
}];
}];
}];
}];
}];
}];
}];
}];
}
- (void)animationShootOut:(UIView *)animationView{
CGRect frame = animationView.frame;
[UIView animateWithDuration:0.4f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^(void){
} completion:^(BOOL finished){
[UIView animateWithDuration:0.4f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(void){
//弹起
animationView.frame = CGRectMake(frame.origin.x, frame.origin.y-20, frame.size.width, frame.size.height);
} completion:^(BOOL finished){
[UIView animateWithDuration:0.4f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^(void){
//下降
animationView.frame = frame;
} completion:^(BOOL finished){
[UIView animateWithDuration:0.4f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(void){
//弹起
animationView.frame = CGRectMake(frame.origin.x, frame.origin.y-10, frame.size.width, frame.size.height);
} completion:^(BOOL finished){
//下降
[UIView animateWithDuration:0.4f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(void){
animationView.frame = frame;
} completion:^(BOOL finished){
}];
}];
}];
}];
}];
}
void shakerAnimation (UIView *view ,NSTimeInterval duration,float height){
CAKeyframeAnimation * animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
CGFloat currentTx = view.transform.ty;
animation.duration = duration;
animation.values = @[@(currentTx), @(currentTx + height), @(currentTx-height/3*2), @(currentTx + height/3*2), @(currentTx -height/3), @(currentTx + height/3), @(currentTx)];
animation.keyTimes = @[ @(0), @(0.225), @(0.425), @(0.6), @(0.75), @(0.875), @(1) ];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[view.layer addAnimation:animation forKey:@"kViewShakerAnimationKey"];
}