如何围绕sourceRect的角落来寻找Peek和Pop 3D Touch?

Sir*_*III 5 ios swift 3dtouch peek-pop

在Safari中,如果使用3D触摸,则正在触摸的链接的sourceRect具有圆角.当我将源矩形设置为:func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {在previewingContext上时,我只能设置previewingContext.sourceRect哪些不允许我绕角,或设置多角区域.我怎样才能做到这一点?

joe*_*ern 5

您可以通过向sourceView图层添加角半径来间接地将圆角设置为sourceRect.当您设置previewingContext.sourceRect为sourceView的边界时,保持焦点的区域也将具有圆角.

以下是使用可压缩UILabel的示例:

class ViewController: UIViewController {

    var previewingContext: UIViewControllerPreviewing?
    let label = UILabel(frame: CGRectMake(150, 250, 100, 50))

    override func viewDidLoad() {
        super.viewDidLoad()

        let background = UIImageView(frame: view.bounds)
        background.image = UIImage(named: "image.jpg")
        view.addSubview(background)

        label.backgroundColor = UIColor.whiteColor()
        label.text = "Press me!"
        label.textAlignment = .Center
        label.layer.cornerRadius = 20
        label.clipsToBounds = true
        label.userInteractionEnabled = true
        view.addSubview(label)

        previewingContext = registerForPreviewingWithDelegate(self, sourceView: label)
    }
}

extension ViewController: UIViewControllerPreviewingDelegate {

    func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
        previewingContext.sourceRect = label.bounds

        return UIViewController()
    }

    func previewingContext(previewingContext: UIViewControllerPreviewing, commitViewController viewControllerToCommit: UIViewController) {
        showViewController(viewControllerToCommit, sender: self)
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 嗨@joem,怎么会为'UITableView`实现这个呢,因为我可能不应该为每个'UITableViewCell`做一个`id <UIViewControllerPreviewing>`,但是想要显示一些圆形视图,(例如,像Apple在消息应用程序中所做的那样; 3D触摸头像) (2认同)