ios - 在 iCarousel 上重用标识符

标签 ios swift xcode icarousel reuseidentifier

我正在尝试基于这个项目在我的应用程序上构建一个轮播,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
}

您可以过滤单元格的 subview 并找到可以转换为 UIImageView 的第一个 subview 。虽然这不是很可靠,但请考虑为此单元格创建一个类和 XIB。

cell.subviews.flatMap { $0 as? UIImageView }.first?.transform = moveImg

关于ios - 在 iCarousel 上重用标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39754076/

相关文章:

ios - Xcode 8.2 模拟器在调试时崩溃并退出保存屏幕截图

iphone - 在 iOS 中显示从当前位置到目的地位置的路线,而无需启动任何 url

ios - Swift 中的 CocoaPods 和 Realm

ios - 尝试向 iOS 设备发送推送通知 - 未收到它们

swift - Alamofire 公钥固定不验证其他 url

ios - 错误 ITMS-90032 :"Invalid Image Path - No image found at the path referenced under key ' CFBundleIcons' :AppIcon40x40"

ios - CameraRoll.saveToCameraRoll() 什么都不做(iOS)

ios - 如何使用 Swift 从右到左和从左到右设置自定义 View 的动画

ios - 在 Swift 中迭代字符串以获取手机号码

c++ - 如何在 lldb 命令行中调用 C++ 对象的公共(public)函数?