Apple 是否有办法将一个视图的外观(即您在屏幕上实际看到的内容)复制到图像缓冲区中?
Does Apple have a way to copy one view's appearance (i.e. what you physically see on screen) into an image buffer?
我们有一个 UIView,当点击时,我们希望 'copy' 屏幕上显示的内容同时向上移动、增大和淡出,作为用户点击该特定项目的视觉提示。将其视为类似于网站上的 'liking' 内容,以赋予它一点视觉效果。 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()
}
)
}
原答案...
知道了!它来自一个没有 Whosebug 帐户的同事 (??!!!) 但它就在这里!完美的!谢谢,乔恩!现在获取一个帐户!!!
复制 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()
})
}
}
是的,它叫做快照视图。
https://developer.apple.com/documentation/uikit/uiview/1622531-snapshotview
我们有一个 UIView,当点击时,我们希望 'copy' 屏幕上显示的内容同时向上移动、增大和淡出,作为用户点击该特定项目的视觉提示。将其视为类似于网站上的 'liking' 内容,以赋予它一点视觉效果。 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()
}
)
}
原答案...
知道了!它来自一个没有 Whosebug 帐户的同事 (??!!!) 但它就在这里!完美的!谢谢,乔恩!现在获取一个帐户!!!
复制 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()
})
}
}
是的,它叫做快照视图。
https://developer.apple.com/documentation/uikit/uiview/1622531-snapshotview