【问题标题】:ASAuthorizationAppleIDButton not responding to touchUpInside eventsASAuthorizationAppleIDButton 没有响应 touchUpInside 事件
【发布时间】:2020-01-26 14:47:00
【问题描述】:

我在最新的 Xcode 11.0 中遇到了奇怪的错误 我的代码:

let button = ASAuthorizationAppleIDButton(type: .default, style: .black)
button.translatesAutoresizingMaskIntoConstraints = false
button.cornerRadius = 10
button.addTarget(self, action: #selector(appleSignInButtonSelected(_:)), for: .touchUpInside)

选择器从未调用过,但如果我将事件更改为 touchDown,一切正常。

【问题讨论】:

  • 你解决过这个问题吗?我也有同样的经历。
  • 在 Xcode 11.2 中仍然是同样的问题
  • 有人解决了吗?

标签: ios swift apple-sign-in


【解决方案1】:

我也遇到过同样的问题。就我而言,我已将UITapGestureRecognizer 附加到视图控制器的主视图。将手势识别器的属性 cancelsTouchesInView 设置为 false 有帮助。

【讨论】:

  • 那是我的问题。我想很多人在他们的登录屏幕上都有一个点击手势识别器,也会遇到这个问题。
  • 似乎这是问题的正确原因,但在我的情况下 cancelsTouchesInView 没有帮助。我在键盘抬起时手动添加了点击手势,并在键盘关闭时删除了点击手势
  • 谢谢伙计,我快疯了!
【解决方案2】:

我认为这是苹果的错误。使用 touchDown 而不是 touchUpInside。

【讨论】:

  • ASAuthorizationAppleIDButtonUIControl;它的行为与真实的UIButton 不同。感谢您的解决方案。
  • 应该是公认的解决方案...直到 Apple 解决这个愚蠢问题
  • 不,这不是 Apple 方面的问题,而是您方面的问题,因为您可能在视图上使用了点击手势。
  • 哇,答案对我来说很棒
【解决方案3】:

我发现 ASAuthorizationAppleIDButton 如果其父级存在布局问题,则根本没有收到任何点击手势(downupInside)。

就我而言,我在其中一个父视图中遗漏了一个约束,这导致ASAuthorizationAppleIDButton 位于父视图的框架之外。

这是在故事板中,所以有一个红色箭头和警告,但我最初错过了。我通过调试视图层次结构并记下树上每个祖先视图的位置发现了这一点。

解决方法是确保有约束将ASAuthorizationAppleIDButton 保持在其祖先的框架内。

【讨论】:

    【解决方案4】:

    编辑 2020-06-16:

    Apple 最近已修复此问题。 AuthorizationAppleIDButtonButton 现在可以触发UIControl.Event.touchUpInside

    虽然与 UIButton 不同,它不会将 UIControl.Event.primaryActionTriggered.touchUpInside 绑定


    原答案:

    如果在Apple's sample app for the sign-in (Juice) 中将touchUpInside 替换为primaryAction,它也不起作用,所以我猜按钮本身有问题。

    除了这个解决方法,我没有找到其他解决方案。该操作将在primaryActionTriggered 上触发,并且登录按钮将显示正常的高亮动画。

    @available(iOS 13.0, *)
    class SignInWithAppleButton: UIButton {
        private var action: (() -> Void)?
        private let authorizationAppleIDButtonButton = ASAuthorizationAppleIDButton()
    
        override var isHighlighted: Bool {
            didSet {
                super.isHighlighted = isHighlighted
                authorizationAppleIDButtonButton.isHighlighted = isHighlighted
            }
        }
    
        class func make(action: @escaping (() -> Void)) -> SignInWithAppleButton {
            let button = SignInWithAppleButton(type: .custom)
            button.setup()
            button.action = action
            return button
        }
    
        private func setup() {
            addSubviewToFill(authorizationAppleIDButtonButton)
            addTarget(self, action: #selector(triggerAction), for: .primaryActionTriggered)
            authorizationAppleIDButtonButton.isUserInteractionEnabled = false
        }
    
        @objc private func triggerAction() {
            action?()
        }
    }
    

    【讨论】:

      【解决方案5】:

      我发现最好的方法就是使用 UITapGestureRecognizer

      let button = ASAuthorizationAppleIDButton()
      let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleAuthorizationAppleIDButtonPress))
      button.addGestureRecognizer(tapGesture)
      

      将 addTarget(_:action:for:) 与 .touchDown 一起使用将需要用户长按按钮(比正常时间长约 1 秒)才能检测到异常操作。

      Apple 甚至拒绝了我提交的应用程序,因为“点击 Sign in with Apple 按钮没有产生任何操作”。

      我建议只使用轻击手势识别器来避免任何问题。

      【讨论】:

        【解决方案6】:

        ASAuthorizationAppleIDButton 的问题在于没有在项目目标中添加“使用 Apple 登录”功能。 Screenshot with capability in XCode

        添加ASAuthorizationControllerDelegate时,需要查看日志:

        @available(iOS 13.0, *)
        extension SignInViewController: ASAuthorizationControllerDelegate {
        
        func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
            print("Error: \(error.localizedDescription)")
        }
        

        }

        【讨论】:

          猜你喜欢
          • 2012-06-13
          • 2021-02-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-10
          • 2011-05-17
          • 2014-03-18
          相关资源
          最近更新 更多