我是swift的新手,我正在通过重新创建Instagram应用程序来学习语言。我的过滤器有问题。当我在模拟器上运行它时,一切正常,除了scrollview。它应该显示所有过滤器,就像它在Instagram应用程序上。但它只显示1个过滤器(最后一个CIFilternames)如何将其他过滤器作为按钮旁边的按钮?
in the Scrollview should be more filters than one
var CIFilterNames = [
"CIPhotoEffectChrome",
"CIPhotoEffectFade",
"CIPhotoEffectInstant",
"CIPhotoEffectNoir",
"CIPhotoEffectProcess",
"CIPhotoEffectTonal",
"CIPhotoEffectTransfer",
"CISepiaTone",
"CIPhotoEffectInstant",
]
override func viewDidLoad() {
super.viewDidLoad()
imageview.image = receivedImage
var xCoord: CGFloat = 5
let yCoord: CGFloat = 10
let buttonWidth:CGFloat = 70
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(x: 5, y: 10, width: 70, height: 70)
filterButton.tag = itemCount
filterButton.addTarget(self, action: #selector(secendTestViewController.filterButtonTapped(sender:)), for: .touchUpInside)
filterButton.layer.cornerRadius = 6
filterButton.clipsToBounds = true
// Create filters for each button
let ciContext = CIContext(options: nil)
let coreImage = CIImage(image: receivedImage)
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!);
filterButton.setBackgroundImage(imageForButton, for: .normal)
// Add Buttons in the Scroll View
xCoord += buttonWidth + gapBetweenButtons
filterScrollView.addSubview(filterButton)
} // END FOR LOOP
// Resize Scroll View
filterScrollView.contentSize = CGSize(buttonWidth * CGFloat(itemCount + 2), yCoord)
}
func filterButtonTapped(sender: UIButton) {
let button = sender as UIButton
imageToFilter.image = button.backgroundImage(for: UIControlState.normal)
}
答案 0 :(得分:2)
我认为你的问题是filterButton框架。 试试这个
filterButton.frame = CGRect(x: xCoord, y: 10, width: 70, height: 70)