我正在尝试根据这个项目https://github.com/nicklockwood/iCarousel在我的应用上构建一个轮播,只有我在swift中写它。我有正确的工作,你可以在这里滚动并点击索引正确的项目:
func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
let tempView = UIView(frame: CGRect(x: 0, y: 130, width: 295, height: 300))
let object = self.imageFilesArray.object(at: index) as! PFObject
let imageView = UIImageView()
imageView.frame = CGRect(x: 0, y: 0 , width: 295, height: 295)
imageView.layer.cornerRadius = 15
imageView.layer.masksToBounds = true
let imageFile = object.object(forKey: "Image") as! PFFile
imageFile.getDataInBackground{
(data, error) -> Void in
if error == nil {
}
imageView.image = UIImage(data: data!)
tempView.addSubview(imageView)
}
return tempView
}
func carousel(_ carousel: iCarousel, didSelectItemAt index: Int) {
let cell = carouselView.itemView(at: index)
let object = self.imageFilesArray[index] as! PFObject
if cell?.tag == 0 {
print("open cell")
let moveImg = CGAffineTransform(translationX: 0, y: -100)
UIView.animate(withDuration: 0.4, delay: 0.1, options: [], animations: {
//cell.imageView.transform = moveImg
}, completion: nil)
cell?.tag = 1
} else if cell?.tag == 1 {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "EventDetailViewController") as! EventDetailViewController
self.present(nextViewController, animated:false, completion:nil)
}
但是,我要做的是当我点击索引处的项目时,imageView会在屏幕上转换。我的问题是我无法访问特定单元格imageView以便移动它。有谁知道我需要改变什么?
答案 0 :(得分:2)
你可以给你的imageView一个这样的标签:
let imageView = UIImageView()
imageView.frame = CGRect(x: 0, y: 0 , width: 295, height: 295)
imageView.layer.cornerRadius = 15
imageView.layer.masksToBounds = true
imageView.tag = 2
然后你就可以从单元格中检索imageView:
if let imageView = cell.viewWithTag(2) as? UIImageView {
imageView.transform = moveImg
}
OR
您可以过滤单元格的子视图,找到可以投射到UIImageView
的第一个子视图。虽然这不是非常强大,但请考虑为此单元格创建一个Class和XIB。
cell.subviews.flatMap { $0 as? UIImageView }.first?.transform = moveImg