- 新建一个demo,在StoryBoard上创建三个VC,如下图:
- 新建两个VC,SecondViewController,和ThirdViewController,分别与StoryBoard的VC相关联。
以SecondViewController举例:
- 该拖线的拖线,添加点击事件。
4.代码部分:
ViewController
//
// ViewController.swift
// ClearBgVC
//
// Created by iOS on 2018/10/16.
// Copyright © 2018年 weiman. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
/// 弹出透明背景的VC,present进来的
@IBAction func jumpToSecondVC(_ sender: Any) {
let vc = SecondViewController.instance()
vc.modalPresentationStyle = .custom
vc.modalTransitionStyle = .crossDissolve
present(vc, animated: true, completion: {})
}
@IBAction func jumpToThirdVC(_ sender: Any) {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) { [weak self] in
if let image = self?.normalSnapshotImage() {
let vc = ThirdViewController.instance(image: image)
self?.navigationController?.pushViewController(vc, animated: true)
}
}
}
}
extension ViewController {
/// 普通截图
func normalSnapshotImage() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(view.frame.size, false, UIScreen.main.scale)
if let context = UIGraphicsGetCurrentContext() {
view.layer.render(in: context)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
return nil
}
}
SecondViewController:
//
// SecondViewController.swift
// ClearBgVC
//
// Created by iOS on 2018/10/16.
// Copyright © 2018年 weiman. All rights reserved.
//
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
setup()
}
private func setup() {
}
class func instance() -> SecondViewController {
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyBoard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
return vc
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
dismiss(animated: true)
}
}
ThirdViewController:
//
// ThirdViewController.swift
// ClearBgVC
//
// Created by iOS on 2018/10/16.
// Copyright © 2018年 weiman. All rights reserved.
//
import UIKit
class ThirdViewController: UIViewController {
@IBOutlet weak var bgImage: UIImageView!
private var image: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
bgImage.image = image
}
class func instance(image: UIImage) -> ThirdViewController {
let storyB = UIStoryboard.init(name: "Main", bundle: nil)
let vc = storyB.instantiateViewController(withIdentifier: "ThirdViewController") as! ThirdViewController
vc.image = image
return vc
}
}
效果: