我正在使用以下代码创建自定义菜单转换.我花了最后两个小时试图弄清楚为什么快照显示为空白(仅使用iPhone 7 Plus模拟器).但是当我想创建一个视频使其成为这个线程的gif时,它可以在我的iPhone 6S Plus上运行.
更新:适用于iPhone 6S模拟器.但仍然没有在7 Plus.
import UIKit
class PresentMenuAnimator : NSObject {
}
extension PresentMenuAnimator : UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.6
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)
let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
let containerView = transitionContext.containerView
containerView.insertSubview((toVC?.view)!, belowSubview: (fromVC?.view)!)
// replace main view with snapshot
let snapshot = fromVC?.view.snapshotView(afterScreenUpdates: false)
snapshot?.tag = MenuHelper.snapshotNumber
snapshot?.isUserInteractionEnabled = false
snapshot?.layer.shadowOpacity = 0.7
containerView.insertSubview(snapshot!, aboveSubview: (toVC?.view)!)
fromVC?.view.isHidden = true
UIView.animate(
withDuration: transitionDuration(using: transitionContext),
animations: {
snapshot!.center.x += UIScreen.main.bounds.width * MenuHelper.menuWidth
},
completion: { _ in
fromVC?.view.isHidden = false
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
)
}
}
Run Code Online (Sandbox Code Playgroud)
iPhone 6S Plus(物理设备)iOS 10
iPhone 7 Plus(模拟器)iOS 10
为什么模拟器上的快照是空白的?
添加此UIView扩展,
public extension UIView {
public func snapshotImage() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, 0)
drawHierarchy(in: bounds, afterScreenUpdates: false)
let snapshotImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return snapshotImage
}
public func snapshotView() -> UIView? {
if let snapshotImage = snapshotImage() {
return UIImageView(image: snapshotImage)
} else {
return nil
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新以下代码,
let snapshot = fromVC?.view.snapshotView(afterScreenUpdates: false)
Run Code Online (Sandbox Code Playgroud)
用,
let snapshot = fromVC?.view.snapshotView()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5660 次 |
| 最近记录: |