【问题标题】:Swift - Button's action doesn’t get triggered unless long tappedSwift - 除非长按,否则不会触发按钮的动作
【发布时间】:2020-12-07 20:50:29
【问题描述】:

我在UIScrollView 中添加了一个Sign in with Apple 按钮,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()
}

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

【问题讨论】:

  • 尝试设置滚动视图的delaysContentTouches = false
  • 试过了,没用
  • 在 iPhone11 / iOS 13.6 上运行时,您的示例的最小实现(我只需要为您的标签假设一个约束 - 以滚动视图为中心)不会重现该问题。触摸按钮后立即发生动作。也许您的层次结构中涉及的任何其他视图导致了问题?您是否尝试过直观地调试视图?
  • 我认为这不是 AutoLayout 的问题,因为在 signInWithAppleButton 的正下方,我还有 2 个从 Storyboard 创建的按钮,它们工作得很好!你真的创建了一个项目吗?能否请您上传,以便我查看?
  • 好的,刚刚创建了一个新项目(单视图应用程序模板),然后在视图控制器中did this

标签: ios uibutton apple-sign-in


【解决方案1】:

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

【讨论】:

  • 所以你的代码有问题,你没有在你的问题中分享它。这似乎不是一个有效的问题。无论如何,希望我的回答能帮助你弄清楚。那么你能把赏金奖励给自己吗? :-)
  • 可以确认这个解决方案。我在 .onTapGesture 键盘关闭时遇到了同样的问题。不确定是否有其他解决方法可以让我们保持键盘关闭手势到位。在 iOS 15 及更高版本中,我知道 .focusable() 可以使用,但这对旧版本的 iOS 没有帮助...
【解决方案2】:

我尝试将 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里面的按钮。在设备上也没有延迟。

【讨论】:

  • 我添加了与您的代码相同的代码,但出现以下错误,但我没有观察到任何延迟 loginWithApple 2020-08-21 12:10:32.943294+0200 numberdice[89163: 3776941] [核心] 授权失败:错误域=AKAuthenticationError Code=-7026 "(null)" UserInfo={AKClientBundleID=com.xxx.xxxxxxxx}。但是,我认为这是您的项目或编码特有的一些问题,您在此处提供的内容无法识别。单击按钮时,我没有观察到任何延迟。
猜你喜欢
  • 1970-01-01
  • 2018-05-15
  • 1970-01-01
  • 2017-07-25
  • 2020-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多