ios - swift 3 : Enlarging images when selected in CollectionView

标签 ios xcode swift3 uicollectionview

美好的一天,我只在 Objective-C 或 Swift 2 中看到过这样的示例,但在运行 Xcode 8 的 Swift 3 中还没有看到。我的情况是我有一个包含一组图像的 Collection View ,我希望它们是当用户点击它们时放大。这是我的代码:

@IBOutlet weak var collectionView: UICollectionView!


var images = ["catPic1.jpg", "catPic2.jpg", "catPic3.jpg"]

override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        collectionView.delegate = self

        collectionView.dataSource = self

    } // end of view did load

 public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return images.count
    }

    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell" , for: indexPath) as! CustomCell

        cell.myImage.image = UIImage(named: images[indexPath.row])


        return cell

    }

顺便说一下,我在另一个 swift 类中有 Collection View 单元格代码。这是代码:

class CustomCell: UICollectionViewCell {

    @IBOutlet weak var myImage: UIImageView!
}

所有图像都正确显示,我只需要在用户选择它们时以某种方式放大它们。谢谢。

最佳答案

您可以使用另一个 UIImageView 来全屏查看您的图像

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let imageView = UIImageView(image: UIImage(named: images[indexPath.row]))
    imageView.frame = self.view.frame
    imageView.backgroundColor = .black
    imageView.contentMode = .top
    imageView.isUserInteractionEnabled = true

    let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
    imageView.addGestureRecognizer(tap)

    self.view.addSubview(imageView)
}

// Use to back from full mode
func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
    sender.view?.removeFromSuperview()
}

关于ios - swift 3 : Enlarging images when selected in CollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43620971/

相关文章:

ios - 如何使用SceneKit用比例尺效果放大粒子图像?

iphone - 掌握 UIviews 和 UIViewcontrollers 的概念

objective-c - encodeWithCoder 什么时候被调用?

ios - Obj-C block 作为参数

ios - Xcode 11.4 不生成自定义意图类

ios - 在segue中添加数据运行时将无法识别SecondViewController类

ios - 请求 Facebook 好友返回空

swift - 如何使用 detailCalloutAccessoryView 更改标注气泡的默认背景颜色

ios - 应用程序非事件状态通知没有到达?

xcode - 什么是 Xcode 中的 Sublimes CMD + D 等价物?