【问题标题】:SWIFT: touchesEnded not called in my tableViewControllerSWIFT:在我的 tableViewController 中未调用 touchesEnded
【发布时间】:2020-12-28 10:34:51
【问题描述】:

所以我有一个名为 SettingsViewController 的 tableviewController,它有以下 touchesEnded 函数:

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        print("yoyoyoyoyoyoyoyQVEWEVIWNE")
        let touchLocation = touch.location(in: view)

        // 290 because the width of the view is 414, and the SettingsViewController width gets set to 0.7 * the view width in SlideInTransition.  0.7 * 414 is 289.8
        if touchLocation.x > 200 {
            dismiss(animated: true)
        }
    }
}

我做了 print 语句来查看它是否被调用,它不是。此视图控制器在自定义转换中呈现“菜单式”幻灯片。我怀疑 UIView 的界限在某种程度上是问题所在。这是自定义转换代码:

class SlideInTransition: NSObject, UIViewControllerAnimatedTransitioning {

var isPresenting: Bool = false

func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
    return 0.5
}

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    
    // Make sure they exist
    
    // The view controller being transitioned from, using the context (ex: here it's the MapViewController)
    guard let fromViewController = transitionContext.viewController(forKey: .from),
    // The view controller being transitioned to, using the context (ex: here it's the SettingsTableViewController)
        let toViewController = transitionContext.viewController(forKey: .to) else {return}
    
    let containerView = transitionContext.containerView
    
    // Constants for appearance of SettingsViewController
    
    let vcWidth = toViewController.view.bounds.width * 0.7
    let vcHeight = toViewController.view.bounds.height
    
    if isPresenting {
        // Add SettingsViewController to container
        containerView.addSubview(toViewController.view)
        
        // Initial frame for view controller, off the screen to the left to start, that way it appears to slide in
        toViewController.view.frame = CGRect(x: -vcWidth, y: 0, width: vcWidth, height: vcHeight)
    }
    
    // Animate view controller onto the screen, sliding in from left
    let transform = {
        toViewController.view.transform = CGAffineTransform(translationX: vcWidth, y: 0)
        }
    
    // Animate back off screen
    let identity = {
        // .identity returns the vc to the initial frame, as created above in the isPresenting if statement
        fromViewController.view.transform = .identity
    }
    
    // Animation of the transition
    let duration = transitionDuration(using: transitionContext)
    let isCancelled = transitionContext.transitionWasCancelled
    UIView.animate(withDuration: duration, animations: {
        // If presenting, transform SettingsViewController (to) onto screen, otherwise set it back off the screen.
        self.isPresenting ? transform() : identity()
    }) { (Bool) in
        transitionContext.completeTransition(!isCancelled)
    }
    
}

}

我编写了 touchesEnded 代码,以便当用户触摸 viewController 外部时,它会关闭(视图控制器仅占屏幕宽度的 70%)但它根本不会被调用,无论我在屏幕上的哪个位置轻敲。知道为什么吗?谢谢。

【问题讨论】:

    标签: ios swift xcode uitableview transition


    【解决方案1】:

    https://developer.apple.com/documentation/uikit/uiresponder/1621084-touchesended

    «如果您在不调用 super 的情况下重写此方法(一种常见的使用模式),您还必须重写其他处理触摸事件的方法,即使您的实现什么也不做。»

    这将是一个开始。

    【讨论】:

    • 如何用 super 来称呼它?不知道把“超级”放在哪里
    • 覆盖 func touchesEnded(_ touches: Set, with event: UIEvent?) { super.touchesEnded(touches:touches, event: event)
    • 感谢 cora,但把它放在 touchesEnded 函数中并没有改变它。如果您有任何其他想法,请lmk。谢谢
    • 好吧,那么我为你推荐这些链接。
    • youtube.com/watch?v=Fj-08kQmcY4 - TableView 具有可区分的数据源,内置预览和菜单。
    猜你喜欢
    • 2020-12-30
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多