如何在ios中实现类似的效果? [关闭]

问题描述 投票:4回答:2

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

ios instagram
2个回答
16
投票

这是一个实现:

- (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 (code from comment)

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
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.