美好的一天,我只在 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/