Apple是否可以将一个视图的外观(即您在屏幕上实际看到的内容)复制到图像缓冲区中?

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

[我们有一个UIView,当您点击时,我们希望屏幕上出现的内容的“副本”同时向上移动,成长和淡出,以作为用户点击该特定项目的视觉提示。可以将其视为类似于“喜欢”网站上的内容以使其具有视觉效果。 Apple是否有任何内置方法将屏幕上视觉上显示的内容检索为图像?

swift uiview uiimage calayer
2个回答

2
投票

简体更新

这是animateUserReaction扩展名的版本,它使用UIView的内置snapshotView功能,不需要makeImageSnapshot辅助功能。

func animateUserReaction() {

    guard let containerView = superview,
          let snapshotView  = snapshotView(afterScreenUpdates:true)
    else {
        return
    }

    // Make sure to match the frame to the current frame
    snapshotView.frame = frame

    containerView.addSubview(snapshotView)

    UIView.animate(
        withDuration : 0.5,
        delay        : 0,
        options      : [.beginFromCurrentState, .curveEaseOut],
        animations: {
            snapshotView.transform       = CGAffineTransform(scaleX: 2, y: 2)
            snapshotView.frame.origin.y -= snapshotView.frame.size.height * 1.5
            snapshotView.alpha           = 0
        },
        completion: { _ in
            snapshotView.removeFromSuperview()
        }
    )
}

原始答案...

知道了!是来自没有StackOverflow帐户(?? !!!)的同事,但是这里是!完善!谢谢,乔恩!现在获得一个帐户!!

复制UIView并存储在UIImage中

extension UIView {

    func makeImageSnapshot() -> UIImage? {
        // NOTE: 0.0 scale is to respect retina (prevents pixelation)
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, 0.0)
        defer {
            UIGraphicsEndImageContext()
        }

        guard let context = UIGraphicsGetCurrentContext() else {
            return nil
        }

        layer.render(in: context)

        return UIGraphicsGetImageFromCurrentImageContext()
    }
}

为副本制作动画...

extension UIView {

    func animateSnapshotOfSelf() {

        guard let containerView = superview,
              let snapshot      = makeImageSnapshot()
        else {
            return
        }

        let imageView = UIImageView(frame: frame)
        imageView.image = snapshot
        containerView.addSubview(imageView)

        UIView.animate(withDuration: 0.75, delay: 0, options: [.beginFromCurrentState, .curveEaseOut], animations: {
            imageView.transform = CGAffineTransform(scaleX: 2, y: 2)
            imageView.frame.origin.y += -100
            imageView.alpha = 0
        }, completion: { _ in
            imageView.removeFromSuperview()
        })
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.