需求 - 18 - “点赞动画”
2016-02-18 11:23
489 查看
coding那个开源项目的“点赞”的动画效果非常赞,既然开源的我就直接转载在自己的博客里啦!
需要准备的:
1.facebook 的pop库:你可以多种方式来达到,推荐的是CocoaPods但是有些人不喜欢用pods,自用工程还好,当需要转给别人的工程pods真的是很难管理,各种版本不一
所以我这里喜欢用直接的pop代码导入:
https://github.com/facebook/pop Github上的源码,这可是有一万多个赞,一千多个关注的明星源码啊
2.点击前后的两张图片
3.用Category来实现
需要注意的是,导入pop会报几个常出现的问题,百度和google很容易得到解决方式
1.Xcode6以后不允许 将integer类型置成nil类型,所以改成0即可
2.需要引入几个库,这个只要根据报错类型也很容易,其实就两个:一个是CFTimeInterval所属的CoreFoundation,
另外一个是runtime的一个宏NS_INLINE,只要导入Foundation这个基础库就可以啦
解决pop库的问题之后,代码就非常简单啦
#import "POPCustomAnimation.h"
#import "UIButton+PopAnimation.h"
@implementation UIButton (PopAnimation)
- (void)animateToImage:(NSString *)imageName
{
UIImage* image = [UIImage imageNamed:imageName];
if (!image)
{
return;
}
[self setImage:image forState:UIControlStateNormal];
if ([self superview])
{
UIView* superView = [self superview];
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
CGRect vFrame = [self convertRect:self.imageView.frame toView:superView];
imageView.frame = vFrame;
[superView addSubview:imageView];
//animation
CGAffineTransform fromTransform = imageView.transform;
CGPoint fromCenter = imageView.center;
CGFloat dx = CGRectGetWidth(self.frame) / 2;
CGFloat dy = CGRectGetHeight(self.frame) * 3;
CGFloat dScale = 3.0;
CGFloat dRotation = M_PI_4;
NSTimeInterval moveDuration = 1.0;
POPCustomAnimation* moveA = [POPCustomAnimation animationWithBlock:^BOOL(id target, POPCustomAnimation* animation){
float time_percent = (animation.currentTime - animation.beginTime) / moveDuration;
UIView* view = (UIView*)target;
//抛物线移动
CGPoint toCenter = CGPointMake(fromCenter.x + time_percent * dx, fromCenter.y - (dy * time_percent * (2 - time_percent)));
view.center = toCenter;
CGAffineTransform toTransform = fromTransform;
toTransform = CGAffineTransformTranslate(toTransform, 50, -50);
//线性放大
CGFloat toScale = 1 + time_percent * (dScale - 1);
toTransform = CGAffineTransformMakeScale(toScale, toScale);
//cos曲线旋转(先慢后快)
CGFloat toRotation = dRotation * (1 - cosf(time_percent * M_PI_2));
toTransform = CGAffineTransformRotate(toTransform, toRotation);
view.transform = toTransform;
view.alpha = 1 - time_percent;
return time_percent < 1.0;
}];
[imageView pop_addAnimation:moveA forKey:@"animateToImage"];
}
}
@end
需要准备的:
1.facebook 的pop库:你可以多种方式来达到,推荐的是CocoaPods但是有些人不喜欢用pods,自用工程还好,当需要转给别人的工程pods真的是很难管理,各种版本不一
所以我这里喜欢用直接的pop代码导入:
https://github.com/facebook/pop Github上的源码,这可是有一万多个赞,一千多个关注的明星源码啊
2.点击前后的两张图片
3.用Category来实现
需要注意的是,导入pop会报几个常出现的问题,百度和google很容易得到解决方式
1.Xcode6以后不允许 将integer类型置成nil类型,所以改成0即可
2.需要引入几个库,这个只要根据报错类型也很容易,其实就两个:一个是CFTimeInterval所属的CoreFoundation,
另外一个是runtime的一个宏NS_INLINE,只要导入Foundation这个基础库就可以啦
解决pop库的问题之后,代码就非常简单啦
#import "POPCustomAnimation.h"
#import "UIButton+PopAnimation.h"
@implementation UIButton (PopAnimation)
- (void)animateToImage:(NSString *)imageName
{
UIImage* image = [UIImage imageNamed:imageName];
if (!image)
{
return;
}
[self setImage:image forState:UIControlStateNormal];
if ([self superview])
{
UIView* superView = [self superview];
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
CGRect vFrame = [self convertRect:self.imageView.frame toView:superView];
imageView.frame = vFrame;
[superView addSubview:imageView];
//animation
CGAffineTransform fromTransform = imageView.transform;
CGPoint fromCenter = imageView.center;
CGFloat dx = CGRectGetWidth(self.frame) / 2;
CGFloat dy = CGRectGetHeight(self.frame) * 3;
CGFloat dScale = 3.0;
CGFloat dRotation = M_PI_4;
NSTimeInterval moveDuration = 1.0;
POPCustomAnimation* moveA = [POPCustomAnimation animationWithBlock:^BOOL(id target, POPCustomAnimation* animation){
float time_percent = (animation.currentTime - animation.beginTime) / moveDuration;
UIView* view = (UIView*)target;
//抛物线移动
CGPoint toCenter = CGPointMake(fromCenter.x + time_percent * dx, fromCenter.y - (dy * time_percent * (2 - time_percent)));
view.center = toCenter;
CGAffineTransform toTransform = fromTransform;
toTransform = CGAffineTransformTranslate(toTransform, 50, -50);
//线性放大
CGFloat toScale = 1 + time_percent * (dScale - 1);
toTransform = CGAffineTransformMakeScale(toScale, toScale);
//cos曲线旋转(先慢后快)
CGFloat toRotation = dRotation * (1 - cosf(time_percent * M_PI_2));
toTransform = CGAffineTransformRotate(toTransform, toRotation);
view.transform = toTransform;
view.alpha = 1 - time_percent;
return time_percent < 1.0;
}];
[imageView pop_addAnimation:moveA forKey:@"animateToImage"];
}
}
@end
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- Gifski:一个跨平台的高质量 GIF 编码器
- 模仿动画的放大缩小容器
- jQuery 1.9.1源码分析系列(十五)动画处理之缓动动画核心Tween
- Android实现定制返回按钮动画效果的方法
- Android中ViewFlipper的使用及设置动画效果实例详解
- jQuery实现美观的多级动画效果菜单代码
- php判断GIF图片是否为动画的方法
- jQuery实现动画效果circle实例
- HTML5游戏引擎LTweenLite实现的超帅动画效果(附demo源码下载)
- 浅析JavaScript动画
- js排序动画模拟-插入排序
- javascript+HTML5的Canvas实现Lab单车动画效果
- 基于javascript实现漂亮的页面过渡动画效果附源码下载