简体   繁体   English

如何根据背景色将图像添加到数组?

[英]How to append images to an array based on their background color?

I have a selected card/image called selectedCard. 我有一个选定的卡/图像,称为selectedCard。 I have an array of UIImageViews called imageArray. 我有一个UIImageViews数组,称为imageArray。 I have a new/empty array of type UIImageViews called newImageArray. 我有一个名为newImageArray的UIImageViews类型的新/空数组。 I am trying to create a function that checks the imageViews in imageArray and if the image is the same as the selectedCard and background color is blue then append the rest of the images with blue background colors into the newImageArray. 我正在尝试创建一个检查imageArray中的imageViews的函数,如果图像与selectedCard相同,并且背景色为蓝色,则将具有蓝色背景色的其余图像附加到newImageArray中。 Else if, check the imageViews in imageArray and if the image is the same as the selectedCard and background color is white then append the rest of the images with white background colors into the newImageArray. 否则,请检查imageArray中的imageViews,如果图像与selectedCard相同,并且背景颜色为白色,则将其余具有白色背景颜色的图像附加到newImageArray中。 I am creating a card app and the user will be presented with 8 cards to choose from. 我正在创建一个卡片应用程序,系统将为用户提供8张卡片供您选择。 The user will need to pick 4 of these cards. 用户将需要选择其中4张卡。 If their selectedCard is apart of the 4 the picked then I want to store all four in an array. 如果他们的selectedCard是所选择的4张卡片中的一部​​分,那么我想将所有4张卡片存储在一个数组中。 If their selectedCard is not apart of the four they selected then I want to store the four non selected cards into the array. 如果他们的selectedCard不在他们选择的四张卡片中,那么我想将四张未选中的卡片存储到阵列中。

I have created an if else statement with a for loop inside but I am not having any luck and not sure what to do at this point. 我创建了一个if语句,里面有一个for循环,但是我没有运气,也不知道该怎么办。

func isCardHighlighted() {
    for imageView in imageArray {
        if imageView.image == selectedCard && imageView.backgroundColor == UIColor.blue {
            for imageView in imageArray {
                if imageView.backgroundColor == UIColor.blue {
                    newImageArray.append(imageView.image!)
                } else {
                    print("Can't add because background is not blue.")
                }
            }
        } else if imageView.image == selectedCard && imageView.backgroundColor == UIColor.white {
            for imageView in imageArray {
                if imageView.backgroundColor == UIColor.white {
                    newImageArray.append(imageView.image!)
                } else {
                    print("Can't add because background is not white.")
                }
            }
        } else {
            print("error")
        }
    }
}

Basically if the selectedCard is highlighted (background is blue) then I want to save it and the other three highlighted images to the newImageArray. 基本上,如果selectedCard被突出显示(背景为蓝色),那么我想将其以及其他三个突出显示的图像保存到newImageArray中。 If the selectedCard is not highlighted (background is white) then I want to save it and the other three images to the newImageArray. 如果selectedCard未突出显示(背景为白色),那么我想将其以及其他三张图像保存到newImageArray中。

I solved the problem I was having. 我解决了我遇到的问题。

Since the newImageArray function was at the top of the class like this var newImageArray: [UIImage]! 由于newImageArray函数位于此类的顶部,就像var newImageArray这样:[UIImage]! I needed to add this-> newImageArray = UIImage to the top of the isCardHighlighted() function.For other new beginners like myself, the reason this needed to be done is because optionals ALWAYS have to be instantiated before you can use them. 我需要在isCardHighlighted()函数的顶部添加this-> newImageArray = UIImage对于其他像我这样的新手来说,需要这样做的原因是因为必须在使用它们之前必须先实例化可选参数。

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

相关问题 依序附加阵列中的图像 - Append images in array in sequence 如何根据背景图像的颜色更改 UILabel 文本颜色? (iOS) - How to change UILabel text color based on the color of background image? (iOS) 如何根据背景颜色自动更改视图的透明颜色 - How to automatically change transparent color of a view based on the color of it’s background 如何使用多个小图像设置UITableViewCell的背景颜色? - How to set UITableViewCell's background color using mutiple small images? 网络通话后,如何将所有图像附加到此阵列? - How do I append all Images to this array after Network call? 如何根据行替换UICollectiveViewCell的背景颜色? - How do I alternate a UICollectiveViewCell's background color based on row? 基于背景图像的文本颜色 - Text color based on background image 如何使用SDWebImage从URL下载图像,然后将这些图像附加到UIImage数组? - How would I download images from URL's with SDWebImage then append those images to a UIImage Array? 如何在iOS Objective-C中基于背景图像颜色更改Label / TextField文本颜色? - how to change the Label/TextField text color Based on background image color in iOS Objective-C? 在数组中更改UILabel的背景颜色 - Change background color of UILabel in Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM