在 iCarousel 上重用标识符
Reuse Identifier on iCarousel
我正在尝试基于这个项目在我的应用程序上构建一个轮播,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 以移动它。有人知道我需要更改什么吗?
你可以给你的 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
}
或
您可以过滤单元格的子视图并找到可以转换为 UIImageView
的第一个子视图。虽然这不是很可靠,但请考虑为此单元格创建 Class 和 XIB。
cell.subviews.flatMap { [=12=] as? UIImageView }.first?.transform = moveImg
我正在尝试基于这个项目在我的应用程序上构建一个轮播,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 以移动它。有人知道我需要更改什么吗?
你可以给你的 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
}
或
您可以过滤单元格的子视图并找到可以转换为 UIImageView
的第一个子视图。虽然这不是很可靠,但请考虑为此单元格创建 Class 和 XIB。
cell.subviews.flatMap { [=12=] as? UIImageView }.first?.transform = moveImg