微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决Swift-除非长时间点按,否则按钮的操作不会被触发

我在Sign in with Apple添加一个UIScrollView按钮,就像苹果文档中所建议的那样;

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,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中的按钮。另外,在设备上也没有延迟。

enter image description here

,

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。