- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个自定义 Collection View 单元格,它使用以下代码向单元格层添加虚线边框:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = appDealCollectionView.dequeueReusableCell(withReuseIdentifier: "appDealCollectionCell", for: indexPath) as! AppDealCollectionViewCell
if let codes = discountCodes {
cell.discountCodeTitle.text = codes[indexPath.row].codeMessageOne
cell.discountCode.text = codes[indexPath.row].code
let yourViewBorder = CAShapeLayer()
yourViewBorder.strokeColor = UIColor.black.cgColor
yourViewBorder.lineWidth = 2
yourViewBorder.lineDashPattern = [10, 10]
yourViewBorder.frame = cell.bounds
yourViewBorder.fillColor = nil
yourViewBorder.path = UIBezierPath(roundedRect: cell.bounds, cornerRadius: 6).cgPath
cell.layer.addSublayer(yourViewBorder)
}
return cell
}
此代码在 View 的初始加载时工作得很好。但是,当方向改变时,单元格大小也会改变。上面的代码确实正确地绘制了新的边框 CAShapeLayer,但是之前绘制的边框图层仍然存在,它是根据旧尺寸绘制的。
结果是两个不同的边界层同时出现,以不同的尺寸相互重叠。
如何使之前绘制的 CAShapeLayers 失效?无效在哪里完成?在 cellForItemAt 中?或者可能在自定义“AppDealCollectionViewCell”本身内部?
最佳答案
由于单元格是可重用的,每次调用 cellForRowAtIndexPath
都会将另一个 CAShapeLayer
实例添加到单元格中。这就是为什么您有几个边界相互重叠的原因。此外,CALayer
既不支持自动布局也不支持 autoresizingMask
,因此您必须手动更新 CAShapeLayer
的大小。
您应该创建 UITableViewCell
的子类,然后创建 CAShapeLayer
的实例并将指向它的指针存储在类属性变量中。一旦发生布局循环,在 layoutSubviews
函数中,您需要更新 CAShapeLayer
的框架。
最终的实现是这样的:
class BorderedTableViewCell: UITableViewCell {
lazy var borderLayer = CAShapeLayer()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupBorderLayer()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupBorderLayer()
}
private func setupBorderLayer() {
borderLayer.strokeColor = UIColor.black.cgColor
borderLayer.lineWidth = 2
borderLayer.fillColor = nil
borderLayer.lineDashPattern = [10, 10]
layer.addSublayer(borderLayer)
}
private func updateBorderLayer() {
borderLayer.frame = bounds
borderLayer.path = UIBezierPath(roundedRect: bounds, cornerRadius: 6).cgPath
}
override func layoutSubviews() {
super.layoutSubviews()
updateBorderLayer()
}
}
希望对您有所帮助。
关于swift - 设备方向更改后使 collectionViewCell 中的 CAShapeLayer 无效并重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51198788/
我有一个自定义 TagCollectionViewCell,它是一个自定义 UICollectionViewCell。在 .xib 时代,我曾经通过 UINib 实例化,但现在使用 Storyboar
不幸的是,我继续遇到约束问题......我绝对是我的问题,但我真的无法理解它们...... 我创建了一个带有自定义单元格的 UICollectionView。 在单元格内,我插入了一个带有所有约束的
在我的应用程序中,我将使用 Collection View ,但我遇到了问题。我在 Storyboard中设计了 Collection View 。我在 View Controller 中插入了一个
我正在使用连接到 CVCell 的 CollectionView 和捏合手势。我在单元格中有一张图像,并且捏合手势放大和缩小图像。我的问题是捏合手势仅适用于偶数行,不适用于奇数行。我的意思是,对于第一
我想隐藏被点击的单元格中的标签,而是显示图像。但我只想在具有特定索引的单元格已设置为 imageView 时才执行此操作。如果单元格设置为 imageView 或不设置,寻址单元格和存储的最佳方式是什
我希望每个 CollectionViewCell 显示图像并在点击时隐藏标签。但是,如果用户滚动图像,则会突然在其他未触摸过的过程中显示。如何识别某些细胞? override func collect
我正在尝试创建一个activityCell,因此当用户到达按钮时,它将显示一个带有事件指示器的单元格。这似乎工作正常,但是如果 moreDataAvailable 为 false 它应该删除此单元格。
我在 CollectionViewCell 中有 2 个标签和 1 个 ImageView 。我可以在单元格中设置图像,但无法在单元格中设置标签。 CollectionViewCell 类: clas
我正在制作一个程序,单个 View Controller 有两个 Collection View 。我已成功填充顶 View Controller ,但在添加第二个 Controller 时,我无法正
我水平滚动 CollectionView 单元格数量未知,它占据了除导航栏之外的整个屏幕。还有 CollectionViewCell,它占用整个 CollectionView。另外,在这个单元格内,我
我有一个布局,它使用多个collectionviewcells来布局。目前我使用的是静态高度,但众所周知,无法确定内容的大小,特别是在我的情况下,因为它将是占据单元格大部分的文本。我无法找到一种准确的
我正在发送从 collectionView 中选择的图像。问题是应用程序总是发送先前选择的图像。 indexPath 有什么问题? 这是 didSelectItemAt 函数: override fu
到目前为止,我一直在成功使用 UITableViews,但我无法让 UICollectionView 工作。 @interface NewsCollectionViewDataSource : NSO
我正在尝试在我的 Collection View 单元格内绘制一个简单的轮廓圆圈。由于某种原因,只有第一个单元格被绘制,其余的没有显示。 class UserCell: UICollectionVie
我使用 Xib 文件创建了一个 Collection View 单元格。我只有一个单元格,但该单元格显示在 collectionView 的中心。我想在 collectionView 的起始位置显示单
我有一个非常简洁的动画,它基本上为 Collection View 单元格绘制了一个边框。 我的动画运行良好,但我无法阻止它无限期地重复。 在:func collectionView(collecti
我一直在寻找学习这个 link 中的 Collection View 一天。在此链接中,它有 viewcontroller.h,.m 和 CustomCollectionCell.h,.m。 他们为什
我有 3 个单元格的 Collection View 。我在它们上面显示图像,但是,因为从服务器获取图像数据需要几秒钟,所以我使用 nsuserdefaults 和缓存。 let imagesFrom
我在使用 uicollectionview 时遇到问题。当我做一个collectionviewcell,在collectionviewcell里做一个uilabel的时候,突然uilabel不见了。
我已经为我的 UICollectionViewCell 设置了一个 UIPanGestureRecognizer,它应该像您经常在 UITableViewCells 中看到的那样工作,显示内容下方的控
我是一名优秀的程序员,十分优秀!