前端之家收集整理的这篇文章主要介绍了
swift中计算图片显示方式返回的尺寸,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
///
图片默认大小 let StausPicktureItemWidth:CGFloat = 90 /// 配图的最多个数 let StatusPicktureMaxCount: CGFloat = 3 /// 配图间距 let StatusPicktureMargin: CGFloat = 5 /// 存放配图的最大宽度 let StatusPicktureMaxWidth: CGFloat = StausPicktureItemWidth * StatusPicktureMaxCount + (StatusPicktureMaxCount - 1) * StatusPicktureMargin /// 存放配图的最大高度 let StatusPicktureMaxHeight: CGFloat = StatusPicktureMaxWidth private func calViewSize()-> CGSize{ //1.取出布局方式 let layout = collectionViewLayout as! UICollectionViewFlowLayout //2.设置item默认大小 layout.itemSize = CGSize(width: StausPicktureItemWidth,height: StausPicktureItemWidth) //3.设置item个数 let count = statusViewModle?.thumbnail_pic?.count ?? 0 //4. 根据item个数,返回size //4.1> 0张 if count == 0 { return CGSizeZero } //4.2> 1张 if count == 1{ let size = CGSizeMake(150,150) layout.itemSize = size return size } //4.3> 4张 2*2 布局方式 if count == 4{ let widthOrHeight = StatusPicktureMaxWidth * 2 + StatusPicktureMargin return CGSize(width: widthOrHeight,height: widthOrHeight) } //4.4> 2,3,5,6,7,8,9张 。2-3一行
显示, 5-6 两行
显示 7-8-9 三行
显示 let row = CGFloat((count - 1) / Int(StatusPicktureMaxCount) + 1) let height = row * StatusPicktureMaxHeight + (row - 1) * StatusPicktureMargin return CGSize(width: StatusPicktureMaxWidth,height: height) }