Peek和Pop功能开发(3D Touch开发之一)

1、哪些设备支持3D Touch

iPhone 6s/Plus、iPhone 7s/Plus、iPhone 8s/Plus、iPhone X
(系统必须是iOS9或者更新的版本)

2、怎么实现Peek和Pop功能

要实现Peek和Pop,必须实现UIViewControllerPreviewingDelegate,此协议只有两个接口

1)Peek接口
public func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController?
2)Pop接口
 public func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController)

实现UIViewControllerPreviewingDelegate协议示例代码

extension ViewController: UIViewControllerPreviewingDelegate {

func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
    
    guard let sorce = storyboard?.instantiateViewController(withIdentifier: "GameViewController") else {
        
        return nil;
    }
    
    // Peek弹出窗口的Size
    sorce.preferredContentSize = CGSize(width: 0.0, height: 500) 
    // 预览区域(你所按位置附近一块区域)
    previewingContext.sourceRect = CGRect(x: 0, y: location.y - 100, width: view.bounds.size.width, height: 100)
    
    print("Peek")
    return sorce
}

func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
    
    show(viewControllerToCommit, sender: self);
    print("Pop")
}
}

此外还需要注册一个视图来响应3D Touch操作

   override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    
    if traitCollection.forceTouchCapability == .available {
        
        // 注册一个View来响应 3D Touch操作(Peek Or Pop)
        registerForPreviewing(with: self, sourceView: view)
        print("你的设备支持3D Touch")
    }
}

3、Demo下载

PeekAndPop

更多更详细教程请访问个人站点: 码出一个长城

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Harvey66

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值