UICollectionViewCell 问题中的 UISwipeGestureRecogniser
UISwipeGestureRecogniser in UICollectionViewCell issue
我有一个 UICollectionView
,其中每个单元格都有一个左右 UISwipeGestureRecognizer
,这给人一种翻转菜单图像的错觉。我不知道该怎么做的是 "closing/flipping" 在翻转另一个单元格之前先翻转了单元格。
这是现在的屏幕截图:
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
})
}
}
您可以为此使用 delegation pattern。例如。您可以在注释 class:
中添加一个协议
protocol FlipDelegate: class {
func willFlip(note: Note)
}
让ViewController符合这个协议,存储最后一张翻转的牌的引用,然后再次翻转最后一张翻转的牌:
weak var lastFlippedNote: Note?
func willFlip(_ note: Note) {
lastFlippedNote?. flipToBackView(options: .transitionFlipFromLeft)
lastFlippedNote = note
}
添加对注释 class 的弱引用,例如 weak var flipDelegate: FlipDelegate?
,并将 viewController 分配给 collectionView(_:cellForItemAt:)
中的单元格。
在您的滑动操作中,您可以调用 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)
}
}
此致
我有一个 UICollectionView
,其中每个单元格都有一个左右 UISwipeGestureRecognizer
,这给人一种翻转菜单图像的错觉。我不知道该怎么做的是 "closing/flipping" 在翻转另一个单元格之前先翻转了单元格。
这是现在的屏幕截图:
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
})
}
}
您可以为此使用 delegation pattern。例如。您可以在注释 class:
中添加一个协议protocol FlipDelegate: class {
func willFlip(note: Note)
}
让ViewController符合这个协议,存储最后一张翻转的牌的引用,然后再次翻转最后一张翻转的牌:
weak var lastFlippedNote: Note?
func willFlip(_ note: Note) {
lastFlippedNote?. flipToBackView(options: .transitionFlipFromLeft)
lastFlippedNote = note
}
添加对注释 class 的弱引用,例如 weak var flipDelegate: FlipDelegate?
,并将 viewController 分配给 collectionView(_:cellForItemAt:)
中的单元格。
在您的滑动操作中,您可以调用 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)
}
}
此致