我正在开发一款社交应用程序,想要实现类似于Instagram的点赞功能。应用程序中有与Instagram相似的图片动态信息流,当用户双击任何一张图片时,应该显示一个带有类似于Instagram的心形图标的动画效果。我尝试了同样的做法,但无法实现这种动画效果。请问有人可以告诉我如何做到这一点吗?
我附上了Instagram点赞功能的图片。
这里是一个实现:
- (void) animateLike {
[UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
heartPopup.transform = CGAffineTransformMakeScale(1.3, 1.3);
heartPopup.alpha = 1.0;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1f delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
heartPopup.transform = CGAffineTransformMakeScale(1.0, 1.0);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
heartPopup.transform = CGAffineTransformMakeScale(1.3, 1.3);
heartPopup.alpha = 0.0;
} completion:^(BOOL finished) {
heartPopup.transform = CGAffineTransformMakeScale(1.0, 1.0);
}];
}];
}];
}
Swift 3.0 代码
func likeAnimation() {
UIView.animate(withDuration: 0.3, delay: 0, options: .allowUserInteraction, animations: {() -> Void in
heartPopup.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
heartPopup.alpha = 1.0
}, completion: {(_ finished: Bool) -> Void in
UIView.animate(withDuration: 0.1, delay: 0, options: .allowUserInteraction, animations: {() -> Void in
heartPopup.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
}, completion: {(_ finished: Bool) -> Void in
UIView.animate(withDuration: 0.3, delay: 0, options: .allowUserInteraction, animations: {() -> Void in
heartPopup.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
heartPopup.alpha = 0.0
}, completion: {(_ finished: Bool) -> Void in
heartPopup.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
})
})
})
}
heartPopup
是一个UIImageView,在界面构建器中将其设置在图像的中心,并将其alpha设置为零。调用上述方法来实现点赞效果的动画。
if let bigLikeImageV = likeImageV, liked == true {
UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.2, options: .allowUserInteraction, animations: {
bigLikeImageV.transform = CGAffineTransform(scaleX: 1.6, y: 1.6)
bigLikeImageV.alpha = 1.0
}) { finished in
bigLikeImageV.alpha = 0.0
bigLikeImageV.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
}
}
layoutSubviews
期间使用CABasicAnimation进行动画处理。然后,当动画完成时,从其superview中删除心形视图(self)。