您现在的位置是:首页 > 文章详情

swift4 透明背景VC

日期:2018-10-15点击:609
  1. 新建一个demo,在StoryBoard上创建三个VC,如下图:
img_62878a025e50fbba0a2ebf52afc8291b.png
image.png
  1. 新建两个VC,SecondViewController,和ThirdViewController,分别与StoryBoard的VC相关联。
    以SecondViewController举例:
img_fc43fffafb3dc3330a8996edf6989673.png
image.png
  1. 该拖线的拖线,添加点击事件。

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 } } 

效果:

img_e4e3dd86c0fc33e8e07477b022a28f37.gif
QQ20181016-132820.gif
原文链接:https://yq.aliyun.com/articles/655158
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章