简体   繁体   English

检测哪个 ImageView 已被点击

[英]Detecting which ImageView has has been tapped

So i have 4 vStacks, each containing 9 ImageViews .所以我有 4 个 vStacks,每个包含 9 个ImageViews Each ImageView represents one Card, alpha 0 is default.每个ImageView代表一张 Card,默认为 alpha 0。 When a Card is Detected (with ARKit ), my code sets the ImageView to alpha 1 so the user can see that the card has been scanned.当检测到卡片时(使用ARKit ),我的代码将ImageView设置为 alpha 1,以便用户可以看到卡片已被扫描。

Now: I want to implement that when the user clicks on one of the ImageViews , an alert should pop up asking the user if he is sure he wants to delete the scanned card.现在:我想实现的是,当用户单击其中一个ImageViews ,应该会弹出一个警报,询问用户是否确定要删除扫描的卡片。 My problem is, I have no idea what the best practice is to get the information that the card has been tapped and how to delete it without hardcoding.我的问题是,我不知道获取卡片已被窃听的信息以及如何在没有硬编码的情况下将其删除的最佳做法是什么。

In ViewDidLoad i set the images into the ImageVies like This:ViewDidLoad我将图像设置为 ImageVies,如下所示:

//This repeats for all 36 ImageViews
imgView1.image = UIImage(named: "Eichel_6")
imgView2.image = UIImage(named: "Eichel_7")
/*When a image is detected with ARKit, this is what happens. Basically
 *it pushes the corresponding reference name to an array called 
 * scannedCards, handles them, and removes them afterwards.
 * spielPoints = gamepoints/points, spielmodus = gamemode
 */

func updateLabel() {
        //print all cards in scanned cards
        for card in scannedCards {
            points += (DataController.shared.spielpoints[DataController.shared.spielmodus!]![card]! * DataController.shared.multCalculator[DataController.shared.spielmodus!]!)
        }
        scannedCards.removeAll()
    }

I am a new to coding, I would be grateful if you correct me if my code snippets are bad, beside my question.我是编码的新手,如果我的代码片段不好,除了我的问题,如果您纠正我,我将不胜感激。 Thank you in advance.先感谢您。

我的 4 个带有 ImageViews 的 vStacks

As has already been mentioned in comments, you should use a UICollectionView for this kind of work.正如评论中已经提到的那样,您应该使用UICollectionView进行此类工作。 @Fogmeister has promised to add an answer concerning that later, so I won't do that. @Fogmeister 已承诺稍后添加有关该问题的答案,因此我不会这样做。 But I can answer the actual question, even though it's not what you should do.但我可以回答实际的问题,即使这不是你应该做的。

From your code I can see that you probably have outlets for all your imageViews (imgView1 ... imgView36) and set each image manually.从您的代码中,我可以看到您可能拥有所有图像视图(imgView1 ... imgView36)的插座并手动设置每个图像。 To detect taps on any of these, you could do something like this:要检测对其中任何一个的点击,您可以执行以下操作:

func viewDidLoad(){
    super.viewDidLoad()
    let allImageViews = [imageView1, imageView2, .... imageView36]
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didTapImageView(gesture:)))
    allImageViews.forEach({$0.addGestureRecognizer(tapGestureRecognizer)})
}

@objc func didTapImageView(gesture:UITapGestureRecognizer){
    guard let imageView = gesture.view as? UIImageView else { return }

    //Here you can put code that will happen regardless of which imageView was tapped.
    imageView.alpha = 0.0

    //If you need to know exactly which imageView was tapped, you can just check
    if imageView == self.imageView1{
        //Do stuff only for imageView1
    }else if imageView == self.imageView2{
        //...
    }//....
}

Again, this is not very good practice.同样,这不是很好的做法。 If you go for UICollectionView instead, you don't have to have outlets for all your imageViews and you don't have to create a gestureRecognizer for handling events.如果您改用 UICollectionView,则不必为所有 imageView 设置插座,也不必创建用于处理事件的gestureRecognizer。 But still, I hope this helped you understand general gestures better.但是,我仍然希望这能帮助您更好地理解一般手势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM