如何在ios中实现类似的效果?

时间:2014-03-03 06:38:01

标签: ios instagram

我正在开发一个社交应用程序,我希望在其中实现类似于instagram的功能,当用户双击任何图像时,有类似于instagram的图像的源,那么它应该显示带有类似于instagram的动画的心脏图标。我尝试做同样的事情但无法实现动画,任何人都可以告诉我该怎么做。 我附上了像功能一样的Instagram形象 enter image description here

2 个答案:

答案 0 :(得分:17)

这是一个实现:

- (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为零。调用上面的方法来为类似效果设置动画。

Swift 4(来自评论的代码)

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)
        }
}

答案 1 :(得分:1)

创建UIView的子类(HEARTShapedView),其中包含仅作为UIBezierPath绘制的心形,添加到CAShapedLayer,并在layoutSubviews期间使用CABasicAnimation为其设置动画。然后,当动画完成后,从它的超视图中删除心形视图(自我)。

要在tableview中使用它,将其作为子视图添加到tableview单元格或图像视图中,视图应在完成后自动动画并将其删除。