Edw*_*onn 5 ios uiswipegesturerecognizer uipangesturerecognizer swift
我正在尝试在我的 collectionViewCell 中实现 UISwipeGestureRecognizer,因此当您向左滑动时,单元格会消失。我想要实现的(我找不到方法)是为滑动设置动画,因此当我向左滑动单元格时,它会以淡入淡出效果消失。这是我在方法内的代码cellForItemAtindexPath
let cSelector = #selector(reset(sender:))
let UpSwipe = UISwipeGestureRecognizer(target: self, action: cSelector)
UpSwipe.direction = UISwipeGestureRecognizerDirection.left
cell.addGestureRecognizer(UpSwipe)
Run Code Online (Sandbox Code Playgroud)
方法
func reset(sender: UISwipeGestureRecognizer) {
let cell = sender.view as! UICollectionViewCell
let i = self.collectionView?.indexPath(for: cell)!.item
self.messages.remove(at: i!)
self.collectionView?.reloadData()
}
Run Code Online (Sandbox Code Playgroud)
谢谢!!!
编辑: 我想我找到了最简单的方法,但我遇到了一些麻烦。我尝试在单元格中实现 UIPanGestureRecognizer。这就是它的样子......
项目单元格
let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(gestureRecognizer:)))
cell.addGestureRecognizer(gestureRecognizer)
Run Code Online (Sandbox Code Playgroud)
方法
func handlePan(gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .began {
// When the drag is first recognized, you can get the starting coordinates here
}
if gestureRecognizer.state == .changed {
let translation = gestureRecognizer.translation(in: self.view)
// Translation has both .x and .y values
if translation.x == translation.x - 100 {
//Method i putted before
reset(sender: gestureRecognizer)
}
//print(translation.x, translation.y)
}
}
Run Code Online (Sandbox Code Playgroud)
我试图找到单元格的坐标,因此当它位于单元格左侧的某个点时,单元格会出现某种淡入淡出动画,然后消失。
有什么帮助吗???谢谢!!!
所以我尝试了这段代码,它对我来说效果很好!
func setupView(){
// Setting up swipe gesture recognizers
let swipeUp : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(userDidSwipeUp(_:)))
swipeUp.direction = .left
collectionView?.addGestureRecognizer(swipeUp)
//let swipeDown : UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(userDidSwipeDown))
//swipeDown.direction = .right
//collectionView?.addGestureRecognizer(swipeDown)
}
func getCellAtPoint(_ point: CGPoint) -> ChatMessageCell? {
// Function for getting item at point. Note optionals as it could be nil
let indexPath = collectionView?.indexPathForItem(at: point)
var cell : ChatMessageCell?
if indexPath != nil {
cell = collectionView?.cellForItem(at: indexPath!) as? ChatMessageCell
} else {
cell = nil
}
return cell
}
func userDidSwipeUp(_ gesture : UISwipeGestureRecognizer) {
let point = gesture.location(in: collectionView) //collectionview
let duration = animationDuration() //0.5
if(cell == nil){
cell = getCellAtPoint(point)
UIView.animate(withDuration: duration, animations: {
//self.activeCell.myCellView.transform = CGAffineTransform(translationX: 0, y: -self.activeCell.frame.height)
self.cell.celdaNormal.transform = CGAffineTransform(translationX: -self.cell.frame.width , y: 0)
})
} else {
// Getting the cell at the point
let cell = getCellAtPoint(point)
// If the cell is the previously swiped cell, or nothing assume its the previously one.
if cell == nil || cell == cell {
// To target the cell after that animation I test if the point of the swiping exists inside the now twice as tall cell frame
let cellFrame = cell?.frame
var rect = CGRect()
if cell != nil {
rect = CGRect(x: (cellFrame?.origin.x)! - (cellFrame?.width)!, y: (cellFrame?.origin.y)!, width: (cellFrame?.width)!*2, height: (cellFrame?.height)!)
}
if rect.contains(point) {
// If swipe point is in the cell delete it
let indexPath = collectionView?.indexPath(for: cell!)
messages.remove(at: indexPath!.row)
collectionView?.deleteItems(at: [indexPath!])
if messages.count == 0 {
reusableView.etiqueta.isHidden = true
}
}
// If another cell is swiped
}
}
func animationDuration() -> Double {
return 0.5
}
Run Code Online (Sandbox Code Playgroud)
您所要做的就是调用setupView()viewDidLoad 中的方法,仅此而已!我不得不提的是,我修改了这个问题的代码...
在 CollectionView 上滑动删除
| 归档时间: |
|
| 查看次数: |
4711 次 |
| 最近记录: |