[英]UISwipeGestureRecogniser in UICollectionViewCell issue
I have a UICollectionView
where each cell has a left and right UISwipeGestureRecognizer
which gives the illusion of flipping an image for a menu. 我有一个
UICollectionView
,其中每个单元格都有一个左右UISwipeGestureRecognizer
,给人一种为菜单翻转图像的错觉。 The thing that I cannot figure out how to do is "closing/flipping" previously flipped cell before flipping another cell. 我不知道该怎么做的事情是在翻转另一个单元格之前先“关闭/翻转”先前翻转的单元格。
Here's screenshot of what it looks like now: 这是现在的屏幕截图:
class Note: UICollectionViewCell {
var isFlipped = false
func addSwipeGestures() {
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swipeLeft(swipe:)))
swipeLeft.direction = UISwipeGestureRecognizer.Direction.left
contentView.addGestureRecognizer(swipeLeft)
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swipeRight(swipe:)))
swipeRight.direction = UISwipeGestureRecognizer.Direction.right
contentView.addGestureRecognizer(swipeRight)
}
@objc func swipeLeft(swipe: UISwipeGestureRecognizer) {
if isFlipped {
flipToBackView(options: .transitionFlipFromLeft)
}
else {
flipToFrontView(options: .transitionFlipFromLeft)
}
}
@objc func swipeRight(swipe: UISwipeGestureRecognizer) {
if isFlipped {
flipToBackView(options: .transitionFlipFromRight)
}
else {
flipToFrontView(options: .transitionFlipFromRight)
}
}
func flipToBackView(options: UIView.AnimationOptions ) {
UIView.transition(with: backView, duration: 0.3, options: options, animations: { [unowned self] in
self.coverImageView.isHidden = false
self.titleLabel.isHidden = false
self.titleView.isHidden = false
self.isFlipped = !self.isFlipped
})
}
func flipToFrontView(options: UIView.AnimationOptions) {
UIView.transition(with: backView, duration: 0.3, options: options, animations: { [unowned self] in
self.coverImageView.isHidden = true
self.titleLabel.isHidden = true
self.titleView.isHidden = true
self.isFlipped = !self.isFlipped
})
}
}
You could use the delegation pattern for this. 您可以为此使用委托模式 。 Eg you could add a protocol to the Note class:
例如,您可以将协议添加到Note类:
protocol FlipDelegate: class {
func willFlip(note: Note)
}
Make the ViewController conform to this protocol, store the reference to the last flipped card and flip the last flipped card again: 使ViewController符合此协议,存储对最后一张翻转的卡的引用,然后再次翻转最后一张翻转的卡:
weak var lastFlippedNote: Note?
func willFlip(_ note: Note) {
lastFlippedNote?. flipToBackView(options: .transitionFlipFromLeft)
lastFlippedNote = note
}
Add a weak reference to the Note class like weak var flipDelegate: FlipDelegate?
向Note类添加一个弱引用,例如
weak var flipDelegate: FlipDelegate?
and assign the viewController to the cell in the collectionView(_:cellForItemAt:)
. 并将viewController分配给
collectionView(_:cellForItemAt:)
的单元格。 In your swipe actions you can call willFlip(_:)
eg when you flip to the front side of your note like: 在滑动操作中,您可以调用
willFlip(_:)
例如,当您翻转到笔记的正面时,例如:
@objc func swipeLeft(swipe: UISwipeGestureRecognizer) {
if isFlipped {
flipToBackView(options: .transitionFlipFromLeft)
}
else {
flipToFrontView(options: .transitionFlipFromLeft)
flipDelegate?.willFlip(note: self)
}
}
@objc func swipeRight(swipe: UISwipeGestureRecognizer) {
if isFlipped {
flipToBackView(options: .transitionFlipFromRight)
}
else {
flipToFrontView(options: .transitionFlipFromRight)
flipDelegate?.willFlip(note: self)
}
}
Regards 问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.