[英]How do I disable multiple button colour selection on ScrollView Items?
我正在開發一個照片編輯器移動應用程序,讓用戶單擊滾動視圖內的項目以在其上方顯示較大的圖像。
問題是,當我點擊多個項目時,它會一直顯示相同的藍色。
我要執行的操作是禁用多個選擇,以便在滾動視圖中的每個項目上一次僅顯示一種顏色。
這是我一直在使用的功能:
func ScrollFunctions() {
var xCoord: CGFloat = 13
let yCoord: CGFloat = 7
let buttonWidth:CGFloat = 60
let buttonHeight: CGFloat = 60
let gapBetweenButtons: CGFloat = 5
var itemCount = 0
for i in 0..<CIFilterNames.count {
itemCount = i
// Button properties
let filterButton = UIButton(type: .custom)
filterButton.frame = CGRect(xCoord, yCoord, buttonWidth, buttonHeight)
filterButton.tag = itemCount
filterButton.addTarget(self, action: #selector(LooksViewController.filterButtonTapped(sender:)), for: .touchUpInside)
filterButton.layer.cornerRadius = 0
filterButton.clipsToBounds = true
// Create filters for each button
currentFilter = CIFilter(name: "\(CIFilterNames[i])" )
let ciContext = CIContext(options: nil)
let coreImage = CIImage(image: imagePassed)
let filter = CIFilter(name: "\(CIFilterNames[i])" )
filter!.setDefaults()
filter!.setValue(coreImage, forKey: kCIInputImageKey)
let filteredImageData = filter!.value(forKey: kCIOutputImageKey) as! CIImage
let filteredImageRef = ciContext.createCGImage(filteredImageData, from: filteredImageData.extent)
let imageForButton = UIImage(cgImage: filteredImageRef!);
// Assign filtered image to the button
filterButton.setBackgroundImage(imageForButton, for: .normal)
// Add Buttons in the Scroll View
xCoord += buttonWidth + gapBetweenButtons
filtersScrollView.addSubview(filterButton)
}
// Resize Scroll View
filtersScrollView.contentSize = CGSize(buttonWidth * CGFloat(itemCount+2), yCoord)
}
當您點擊滾動視圖項目時,這是該功能。 在這里,我一直在嘗試'if else'語句,例如,如果按鈕處於選中狀態,則顯示邊框的寬度和顏色,否則返回false,但這沒有用。 有任何想法嗎?
@objc func filterButtonTapped(sender: UIButton) {
let button = sender as UIButton
button.layer.borderWidth = 2
button.layer.borderColor = #colorLiteral(red: 0.1882352941, green: 0.6392156863, blue: 0.6705882353, alpha: 1)
imgView.image = imagePassed
toolBar.isHidden = true
yesAndNoToolBar.isHidden = false
imgView.image = button.backgroundImage(for: UIControlState.normal)
}
好的,您可以采用如下所示的一個成員變量。
var lastSelectedButton: UIButton?
現在在filterButtonTapped(sender: UIButton)
函數中檢查lastSelectedButton
let button = sender as UIButton
if let button1 = lastSelectedButton {
// Deselect or reset this button1 color.
}
lastSelectedButton = button
// Do your stuff here what you are doing now.
如果有任何疑問,請評論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.