I have a collection view cell:
class MyTripsCollectionViewCell: UICollectionViewCell, UIGestureRecognizerDelegate {
that implements the UIGestureRecognizerDelegate
I added pan gesture recognizer :
override func awakeFromNib() {
let gestureRecognizer = UIPanGestureRecognizer(target: self, action:#selector(MyTripsCollectionViewCell.handlePan(_:)))
gestureRecognizer.delegate = self
self.addGestureRecognizer(gestureRecognizer)
}
below is the handlePane Function :
func handlePan(recognizer: UIPanGestureRecognizer) {
if (recognizer.state == UIGestureRecognizerState.Began) {
// if the gesture has just started, record the current centre location
originalCenter = self.center
}
if (recognizer.state == UIGestureRecognizerState.Changed) {
// translate the center
if recognizer.isLeft(self) {
let translation = recognizer.translationInView(self)
if self.center.x >= 150 {
self.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y);
}
// determine whether the item has been dragged far enough to initiate a delete / complete
deleteOnDragRelease = self.frame.origin.x == 150 ? false : true
}
}
// 3
if (recognizer.state == UIGestureRecognizerState.Ended) {
// the frame this cell would have had before being dragged
let originalFrame = CGRectMake(0, self.frame.origin.y,
self.bounds.size.width, self.bounds.size.height);
if (!deleteOnDragRelease) {
// if the item is not being deleted, snap back to the original location
UIView.animateWithDuration(0.2) {
self.frame = originalFrame;
}
}
}
}
In this code I am trying to move the collection view cell from left to right,until it's cente reach 150 point, this work now when I move my finger slowly, but the problem is when I move my finger very fast the translation take a large value which make the cell move more than 150
and then stop cause it make the condition be true
so how I can fix the problem of the speed of panning?
Aucun commentaire:
Enregistrer un commentaire