前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >IOS UICollectionView 图片展示墙

IOS UICollectionView 图片展示墙

作者头像
用户5760343
发布2019-07-08 11:54:42
2.3K0
发布2019-07-08 11:54:42
举报
文章被收录于专栏:sktj
代码语言:javascript
复制
//put the images in to project
 import UIKit
 2
 3 class ViewController:UIViewController,
 UICollectionViewDataSource, UICollectionViewDelegate {
 4
 5 var images = [“Pic1”, “Pic2”, “Pic3”, “Pic8”, “Pic5”,
 “Pic6”, “Pic7”, “Pic4”, “Pic9”]
 6
 7 override func viewDidLoad() {
 8 super.viewDidLoad()
 9 // Do any additional setup after loading the view,
 typically from a nib.
 10 let screenRect = UIScreen.main.bounds
 11 let rect = CGRect(x:0, y:20, width:
 screenRect.size.width, height:screenRect.size.height - 20)
 12
 13 let flowLayout = UICollectionViewFlowLayout()
 14 flowLayout.itemSize = CGSize(width:155, height:
 15 flowLayout.scrollDirection =
 UICollectionViewScrollDirection.vertical
 16
 17 let collectionView = UICollectionView(frame:rect,
 collectionViewLayout:flowLayout)
 18
 19 collectionView.dataSource = self
 20 collectionView.delegate = self
 21
 collectionView.register(UICollectionViewCell.classForCoder(),
 forCellWithReuseIdentifier: “reusedCell”)
 22 self.view.addSubview(collectionView)
 23 }
 24
 25 func collectionView(_ collectionView:
 UICollectionView, numberOfItemsInSection section:Int) ->
 Int {
 26 return images.count
 27 }
 28
 29 func collectionView(_ collectionView:
 UICollectionView, cellForItemAt indexPath:IndexPath) ->
 UICollectionViewCell {
 30
 31 let identifier = “reusedCell”
 32 let cell =
 collectionView.dequeueReusableCell(withReuseIdentifier:
 identifier, for:indexPath)
 33
 34 let imageView:UIImageView?= cell.viewWithTag(1)
 as?UIImageView
 35 if imageView == nil{
 36 let image = UIImage(named:images[(indexPath as
 NSIndexPath).row])
 37 let imageView = UIImageView(image:image)
 38 imageView.frame = CGRect(x:0, y:0, width:150,
 height:135)
 39 imageView.layer.opacity = 0.5
 40 imageView.tag = 1
 41 cell.addSubview(imageView)
 42 }else{
 43 imageView?.image = UIImage(named:
 images[(indexPath as NSIndexPath).row])
 44 }
 45
 46 return cell
 47 }
 48
 49 func collectionView(_ collectionView:
 UICollectionView, didSelectItemAt indexPath:IndexPath) {
 50 let cell = collectionView.cellForItem(at:indexPath)
 51 let imageView = cell?.viewWithTag(1)
 52 imageView?.layer.opacity = 1.0;
 53 }
 54 }

image.png

代码语言:javascript
复制
//UICollectionView包括三个部分:Cells、Supplementary Views、Decoration Views

image.png

image.png

image.png

image.png

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.06.02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档