为什么模拟器中按钮无法点击?

问题描述 投票:0回答:1

我正在构建一个显示折扣卡的应用程序,例如我当地企业的名片。

这是我的 CardData.swift:

import Foundation
import UIKit

struct CardData {
        let image: UIImage
        let facebookUrl: URL?
        let instagramUrl: URL?
        let websiteUrl: URL?
        let directionsUrl: URL?
}

class CustomCollectionViewCell: UICollectionViewCell {
    var imageView: UIImageView!
    var facebookButton: UIButton!
    var instagramButton: UIButton!
    var websiteButton: UIButton!
    var directionsButton: UIButton!
    var buttonStackView: UIStackView!
    
    var cardData: CardData? {
        didSet {
            updateUI()
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupViews() {
            // ImageView setup
            imageView = UIImageView(frame: bounds)
            imageView.contentMode = .scaleToFill
            imageView.clipsToBounds = true
            addSubview(imageView)

            // Create transparent buttons
            facebookButton = createTransparentButton()
            instagramButton = createTransparentButton()
            websiteButton = createTransparentButton()
            directionsButton = createTransparentButton()
            facebookButton.isUserInteractionEnabled = true
            instagramButton.isUserInteractionEnabled = true
            websiteButton.isUserInteractionEnabled = true
            directionsButton.isUserInteractionEnabled = true

            // Calculate button frame dimensions
            let buttonWidth: CGFloat = bounds.width / 4 // Divide width equally for 4 buttons
            let buttonHeight: CGFloat = 41.72  // Height of the buttons

            // Set button frames
            facebookButton.frame = CGRect(x: 0, y: bounds.height - buttonHeight, width: buttonWidth, height: buttonHeight)
            instagramButton.frame = CGRect(x: buttonWidth, y: bounds.height - buttonHeight, width: buttonWidth, height: buttonHeight)
            websiteButton.frame = CGRect(x: 2 * buttonWidth, y: bounds.height - buttonHeight, width: buttonWidth, height: buttonHeight)
            directionsButton.frame = CGRect(x: 3 * buttonWidth, y: bounds.height - buttonHeight, width: buttonWidth, height: buttonHeight)

            // Setting up the stack view for buttons
            buttonStackView = UIStackView(arrangedSubviews: [facebookButton, instagramButton, websiteButton, directionsButton])
            buttonStackView.distribution = .fillEqually
            buttonStackView.alignment = .fill
            buttonStackView.axis = .horizontal
            buttonStackView.frame = CGRect(x: 0, y: bounds.height - buttonHeight, width: bounds.width, height: buttonHeight)
            imageView.addSubview(buttonStackView)  // Add stack view to the imageView


            // Bring button stack view to the front
            bringSubviewToFront(buttonStackView)
        print("Facebook button frame: \(facebookButton.frame)")
        print("Instagram button frame: \(instagramButton.frame)")
        print("Website button frame: \(websiteButton.frame)")
        print("Directions button frame: \(directionsButton.frame)")


    }

    private func createTransparentButton() -> UIButton {
        let button = UIButton(type: .system)
        button.backgroundColor = .clear  // Transparent button
        button.tintColor = .clear  // No tint color
        return button
    }
    
    private func updateUI() {
        guard let data = cardData else { return }
        imageView.image = data.image
        
            // Manage button visibility and actions based on URL availability
        updateButtonVisibilityAndAction(facebookButton, url: data.facebookUrl)
        updateButtonVisibilityAndAction(instagramButton, url: data.instagramUrl)
        updateButtonVisibilityAndAction(websiteButton, url: data.websiteUrl)
        updateButtonVisibilityAndAction(directionsButton, url: data.directionsUrl)
        print("Facebook URL: \(data.facebookUrl?.absoluteString ?? "N/A")")
            print("Instagram URL: \(data.instagramUrl?.absoluteString ?? "N/A")")
            print("Website URL: \(data.websiteUrl?.absoluteString ?? "N/A")")
            print("Directions URL: \(data.directionsUrl?.absoluteString ?? "N/A")")
    }
    
    private func updateButtonVisibilityAndAction(_ button: UIButton, url: URL?) {
            button.isHidden = url == nil  // Hide if URL is nil
            
            // Log a message if the button is hidden
            if button.isHidden {
                    print("No Button Shown")
            } else {
                    print("Button Displayed")
            }
            
            if let actualURL = url {
                    button.addAction(UIAction { [weak self] action in
                            print("Button tapped") // Add this line to print a message when the button is tapped
                            self?.openURL(actualURL)
                    }, for: .touchUpInside)
            } else {
                    button.removeTarget(self, action: nil, for: .allEvents)  // Remove any existing actions
            }
    }
    
    private func openURL(_ url: URL) {
            // Attempt to open the URL directly if it's an HTTP or HTTPS URL
            if ["http", "https"].contains(url.scheme?.lowercased() ?? "") {
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                    return
            }
            
            // Handle specific app URLs
            if let host = url.host?.lowercased() {
                    switch host {
                    case "facebook.com":
                            let appURL = URL(string: "fb://facewebmodal/f?href=\(url.absoluteString)")!
                            if UIApplication.shared.canOpenURL(appURL) {
                                    UIApplication.shared.open(appURL, options: [:], completionHandler: nil)
                                    return
                            }
                    case "instagram.com":
                            let appURL = URL(string: "instagram://app/\(url.absoluteString)")!
                            if UIApplication.shared.canOpenURL(appURL) {
                                    UIApplication.shared.open(appURL, options: [:], completionHandler: nil)
                                    return
                            }
                    case "maps.google.com":
                            let appURL = URL(string: "comgooglemaps://?daddr=\(url.query ?? "")")!
                            if UIApplication.shared.canOpenURL(appURL) {
                                    UIApplication.shared.open(appURL, options: [:], completionHandler: nil)
                                    return
                            }
                    default:
                            break
                    }
            }
            
            // Open the original URL if no specific handling was done
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }

}

我必须删除 ActivitesVC.swift 中的链接,因为 Stack Overflow 说它是垃圾邮件,但它们确实在日志中正确显示。

这是我的ActivityVC.swift:

import SwiftUI
import Foundation
import UIKit

class Activities: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    var collectionView: UICollectionView!
    var cards: [CardData] = [] // Array to hold card data
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupCollectionView()
        loadCards() // Function to load card data
    }
    
    private func setupCollectionView() {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: view.frame.width, height: 144) // Adjust based on your cell size
        collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
        collectionView.register(CustomCollectionViewCell.self, forCellWithReuseIdentifier: "CustomCell")
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.backgroundColor = .white
        view.addSubview(collectionView)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return cards.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as? CustomCollectionViewCell else {
            fatalError("Expected `CustomCollectionViewCell` type for reuseIdentifier 'CustomCell'.")
        }
        cell.cardData = cards[indexPath.row]
        return cell
    }
    
    private func loadCards() {
            // Define an array of dictionaries, where each dictionary represents the data for a single card
            let cardDataArray: [[String: Any]] = [
                    [
                            "imageName": "Blank Card Template",
                            "facebookUrl": "Specific url",
                            "instagramUrl": "Specific url",
                            "websiteUrl": "Specific url",
                            "directionsUrl": "Specific url"
                    ],
                    [
                            "imageName": "Blank Card Template",
                            "facebookUrl": "Specific url",
                            "instagramUrl": "Specific url",
                            "websiteUrl": "Specific url",
                            "directionsUrl": "Specific url"
                    ]
                    // Add more dictionaries as needed for additional cards
            ]

            // Iterate through the array of dictionaries and create CardData objects
            for cardDataDict in cardDataArray {
                    if let imageName = cardDataDict["imageName"] as? String,
                         let image = UIImage(named: imageName),
                         let facebookUrlString = cardDataDict["facebookUrl"] as? String,
                         let facebookUrl = URL(string: facebookUrlString),
                         let instagramUrlString = cardDataDict["instagramUrl"] as? String,
                         let instagramUrl = URL(string: instagramUrlString),
                         let websiteUrlString = cardDataDict["websiteUrl"] as? String,
                         let websiteUrl = URL(string: websiteUrlString),
                         let directionsUrlString = cardDataDict["directionsUrl"] as? String,
                         let directionsUrl = URL(string: directionsUrlString) {

                            // Create a CardData object and append it to the cards array
                            let card = CardData(image: image, facebookUrl: facebookUrl, instagramUrl: instagramUrl, websiteUrl: websiteUrl, directionsUrl: directionsUrl)
                            cards.append(card)
                    } else {
                            print("Failed to create CardData from dictionary: \(cardDataDict)")
                    }
            }
    }
}

Here is my storyboard navigator:

我将它设置在 UIViewController 中,它应该将数据加载到 CollectionViewCells 中,它确实做到了,每个按钮都有基于日志的正确 URL:

Facebook 按钮框架:(0.0, 102.28, 98.25, 41.72)
Instagram 按钮框架:(98.25, 102.28, 98.25, 41.72)
网站按钮框架:(196.5, 102.28, 98.25, 41.72)
方向按钮框:(294.75, 102.28, 98.25, 41.72)
显示按钮
显示按钮
显示按钮
显示按钮
Facebook URL:“具体网址”
Instagram 网址:“具体网址”
网站网址:“具体网址”
路线网址:“具体网址”
Facebook 按钮框架:(0.0, 102.28, 98.25, 41.72)
Instagram 按钮框架:(98.25, 102.28, 98.25, 41.72)
网站按钮框架:(196.5, 102.28, 98.25, 41.72)
方向按钮框:(294.75, 102.28, 98.25, 41.72)
显示按钮
显示按钮
显示按钮
显示按钮
Facebook URL:“具体网址”
Instagram 网址:“具体网址”
网站网址:“具体网址”
路线网址:“具体网址”

但我无法点击应用程序内的按钮。

Scene inside the simulator

swift uicollectionview uikit storyboard
1个回答
0
投票

imageView 的 isUserInteractionEnabled 默认值为 false。因此子视图按钮点击将被忽略。

在 setupViews() 中设置 imageView.isUserInteractionEnabled = true。现在点击按钮就可以了。

© www.soinside.com 2019 - 2024. All rights reserved.