3DTouch以编程方式查看和弹出UIButton

时间:2016-03-13 08:58:02

标签: ios swift uibutton 3dtouch

所以我有一个ViewController,它显示一个x y图,每个点都是一个以编程方式UIButton,按下它时会显示一个警告。我想知道是否有可能使用3DTouch peek和pop在UIButton中用力按压,它会将你发送到另一个视图。

1 个答案:

答案 0 :(得分:0)

首先,您需要注册预览:

if traitCollection.forceTouchCapability == .available {
        registerForPreviewing(with: self, sourceView: view)
    }

将委托添加到ViewController

class ViewController: UIViewController, UIViewControllerPreviewingDelegate {

然后添加委托方法:

func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
    let stboard = UIStoryboard(name: "HomeScreen", bundle: nil)
    guard let detailVC = stboard.instantiateViewController(withIdentifier: "DetailViewController") as? SecondVC else { return nil }
    // 1
    let pressedView = tappedView(location)
    previewingContext.sourceRect = pressedView.frame

    return detailVC
}

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

在(1)中,因为你有第二个视图控制器的实例,你可以在这里轻松传递数据。

我不知道你是如何管理你的按钮的,但我有一个类似的场景(随机的UIViews在屏幕上移动),所以只要在MainView中加载一个View我就把它附加到一个数组中观点。

viewArray.append(newView)

然后我创建了一个方法来检查视图我按

func tappedView(_ location: CGPoint) -> UIView{

    var tappedVw = UIView()

    for vw in viewArrays{
        if vw.frame.contains(location){
            tappedVw = vw as! UIView
        }
    }
    return tappedVw
}

希望这有帮助