您的位置:首页 > 移动开发 > IOS开发

需求 - 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS pop 动画 需求