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