在微信小程序首页,从最左边缘往右滑动,页面就会往下拉动,到达屏幕一半时,自动关闭页面。 直接上代码:
import UIKit
///导航控制器 MyNavVc.swift
public class MyNavVc: UINavigationController {
public override func viewDidLoad() {
super.viewDidLoad()
let edgePanGesture = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(self.gest(sender:)))
edgePanGesture.edges = .left
self.view.addGestureRecognizer(edgePanGesture)
}
@objc private func gest(sender:UIScreenEdgePanGestureRecognizer){
let point = sender.location(in: self.view)
switch sender.state {
case .changed:
self.view.transform = CGAffineTransform(translationX: 0, y: point.x)
case .cancelled,.ended,.failed:
if(point.x >= self.view.bounds.height / 2){
self.dismiss(animated: true, completion: nil)
}else{
self.view.transform = CGAffineTransform.identity
}
default:
break
}
}
}
打开页面:
@objc private func test(){
let vc = MyNavVc(rootViewController: UIViewController())
vc.modalPresentationStyle = .overFullScreen
self.present(vc, animated: true, completion: nil)
}
注意一点: vc.modalPresentationStyle = .overFullScreen 目的是让页面往下拉动,可以看到后面页面,就是页面穿透的效果。