我正在尝试按照这篇文章的教程:How to implement UITableView`s swipe to delete for UICollectionView完全一样,除了从故事板添加插件外,我已经以编程方式初始化了所有内容(包括我的集合视图)。
当我到达包含集合视图的屏幕时,不会显示用于显示其下方删除按钮的滑动操作。我已设置委托方法shouldRecognizeSimultaneouslyWith返回true并设置了单元格如下:
import UIKit
class CustomCollectionViewCell: UICollectionViewCell {
// Cell data
var aLabel : UILabel!
var bLabel : UILabel!
var myImageView : UIImageView!
var myContentView: UIView!
// Swipe to delete cell
var contentViewLeftConstraint : NSLayoutConstraint!
var contentViewRightConstraint : NSLayoutConstraint!
var deleteButton : UIButton!
var startPoint = CGPoint()
var startingRightLayoutConstraintConstant = CGFloat()
var swipeView : UIView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
backgroundColor = .clear
myImageView = UIImageView(frame: CGRect(
x : 15,
y : 10,
width : frame.height - 20,
height: frame.height - 20
))
aLabel = UILabel(frame: CGRect(
x : myImageView.frame.maxX + 15,
y : 15,
width : frame.width - 20 - myImageView.frame.maxX,
height: 23
))
bLabel = UILabel(frame: CGRect(
x : myImageView.frame.maxX + 15,
y : aLabel.frame.maxY,
width : frame.width - 20 - myImageView.frame.maxX,
height: 30
))
swipeView = UIView(frame: CGRect(
x : contentView.frame.width - 60,
y : contentView.frame.minY,
width : 60,
height: contentView.frame.height
))
deleteButton = UIButton(frame: swipeView.frame)
deleteButton.backgroundColor = .red // For visibility purposes
swipeView.addSubview(deleteButton)
myContentView = UIView(frame: contentView.frame)
myContentView.addSubview(myImageView)
myContentView.addSubview(aLabel)
myContentView.addSubview(bLabel)
contentView.addSubview(swipeView)
contentView.addSubview(myContentView)
// Pin the song data content view's left margin to the cell content view's left margin.
contentViewLeftConstraint = NSLayoutConstraint(
item : myContentView,
attribute : .left,
relatedBy : .equal,
toItem : contentView,
attribute : .leftMargin,
multiplier: 1.0,
constant : 0.0
)
contentViewLeftConstraint.isActive = true
// Pin the song data content view's right margin to the cell content view's right margin.
contentViewRightConstraint = NSLayoutConstraint(
item : myContentView,
attribute : .right,
relatedBy : .equal,
toItem : contentView,
attribute : .rightMargin,
multiplier: 1.0,
constant : 0.0
)
contentViewRightConstraint.isActive = true
}
}
我的ViewController方法与教程中的方法完全相同。我在Ray Wenderlich的原始教程中打印出平移动作,并且平移手势表现得如预期的那样。只是myContentView
向左滑动以显示删除按钮。也许我错误地设置了约束?
请帮忙!