swift4 透明背景VC

本文介绍如何在SwiftUI中使用Storyboard创建并跳转到SecondViewController和ThirdViewController,展示了如何通过代码实现弹出透明背景的VC以及利用DispatchQueue进行异步推送,并附有示例代码。
  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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值