Swift - 除非长按,否则不会触发按钮的操作

Swift - Button's action doesn’t get triggered unless long tapped

我按照 Apple 文档的建议在 UIScrollView 中添加了一个 Sign in with Apple 按钮;

let signInWithAppleButton = ASAuthorizationAppleIDButton(type: .default, style: .white)
            
signInWithAppleButton.addTarget(self, action: #selector(loginWithApple(_:)), for: .touchUpInside)
signInWithAppleButton.cornerRadius = 12
            
scrollView.addSubview(signInWithAppleButton)

事实是,该按钮仅响应长时间的按下而不是简单的点击。我试过将它放在 UIScrollView 之外,它在那里工作!

这是我的 UIScrollView 和按钮设置;

fileprivate func setupScrollViewConstraints() {
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    
    NSLayoutConstraint.activate([
        scrollView.topAnchor.constraint(equalTo: self.view.topAnchor),
        scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
        scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
        scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
    ])
}

func setupSignInWithAppleButton() -> UIControl? {
    if #available(iOS 13.0, *) {
        let signInWithAppleButton = ASAuthorizationAppleIDButton(type: .default, style: .white)
        
        signInWithAppleButton.addTarget(self, action: #selector(loginWithApple(_:)), for: .touchUpInside)
        signInWithAppleButton.cornerRadius = 12
        
        scrollView.addSubview(signInWithAppleButton)
        
        signInWithAppleButton.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            signInWithAppleButton.topAnchor.constraint(equalTo: accountLabel.bottomAnchor, constant: 8),
            signInWithAppleButton.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
            signInWithAppleButton.widthAnchor.constraint(equalToConstant: 210),
            signInWithAppleButton.heightAnchor.constraint(equalToConstant: 45)
        ])
        
        return signInWithAppleButton
    }
    
    return nil
}

知道是什么破坏了它吗?

编辑;这是我的处理程序;

@objc private func loginWithApple(_ sender: Any) {
    let appleIDProvider = ASAuthorizationAppleIDProvider()
        
    let request = appleIDProvider.createRequest()
    request.requestedScopes = [.fullName, .email]
        
    let authorizationController = ASAuthorizationController(authorizationRequests: [request])
        
    authorizationController.delegate = self
    authorizationController.presentationContextProvider = self
    authorizationController.performRequests()
}

我已经在其中设置了一个断点,但只有长按按钮才会激活它!提醒一下,如果我将按钮放在控制器的视图中,按钮将按预期工作!

我尝试将 signInWithAppleButton 添加到 UIScorllview,如下所示:

@objc func loginWithApple(_ sender: Any) {
    print("loginWithApple")
}

fileprivate func setupScrollViewConstraints() {
    scrollView.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint.activate([
        scrollView.topAnchor.constraint(equalTo: self.view.topAnchor),
        scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
        scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
        scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
    ])
}

func setupSignInWithAppleButton() -> UIControl? {
    if #available(iOS 13.0, *) {
        let signInWithAppleButton = ASAuthorizationAppleIDButton(type: .default, style: .white)

        signInWithAppleButton.addTarget(self, action: #selector(loginWithApple(_:)), for: .touchUpInside)
        signInWithAppleButton.cornerRadius = 12

        scrollView.addSubview(signInWithAppleButton)

        signInWithAppleButton.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([
            signInWithAppleButton.centerYAnchor.constraint(equalTo: scrollView.centerYAnchor),
            signInWithAppleButton.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
            signInWithAppleButton.widthAnchor.constraint(equalToConstant: 210),
            signInWithAppleButton.heightAnchor.constraint(equalToConstant: 45)
        ])

        return signInWithAppleButton
    }

    return nil
 }

我没有遇到任何问题,当点击按钮时,会立即为我打印“loginWithApple”。该按钮的行为与任何其他 UIButton 一样。我没有发现任何问题。

你在 loginWithApple 做什么?使用您提供的最少代码,我预计 loginWithApple 函数中可能存在一些问题,可能会延迟您的预期。

更新: 以下是行为。请注意 UIScrollView 中的按钮。在设备上也没有延迟。

我终于想通了!!!我在我的滚动视图上添加了一个点击手势,所以我可以在点击时关闭键盘!由于某种原因,它只破坏了 Apple 的登录按钮触摸事件!