完全を期すために、私が使用したコードは次のとおりです。
- (void)swipeCell:(id)sender
{
UIPanGestureRecognizer *gesture = (UIPanGestureRecognizer *)sender;
CGPoint point = [gesture translationInView:self];
if ([gesture state] == UIGestureRecognizerStateBegan)
{
// CGPoint initialPosition
_initialPosition = _topView.frame.origin;
}
if ([gesture state] == UIGestureRecognizerStateChanged)
{
// Hacky Elastic method
float newPosition = _initialPosition.x + point.x;
// Starting point
if (_initialPosition.x == 0)
{
// Once we get to our pre-defined kDeleteViewWidth, start dividing by 5
if (_topView.frame.origin.x < -kDeleteViewWidth) {
float distance = (point.x - -kDeleteViewWidth);
float newDistance = distance / 5;
newPosition = (-kDeleteViewWidth + newDistance);
}
else if (_topView.frame.origin.x > 0)
{
float distance = point.x;
float newDistance = distance / 5;
newPosition = newDistance;
}
}
else if (_topView.frame.origin.x < -kDeleteViewWidth)
{
float distance = point.x;
float newDistance = distance / 5;
newPosition = (-kDeleteViewWidth + newDistance);
}
else if (_topView.frame.origin.x > 0)
{
float distance = point.x + _initialPosition.x;
float newDistance = distance / 5;
newPosition = newDistance;
}
[_topView setFrame:CGRectMake(newPosition, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
}
if ([gesture state] == UIGestureRecognizerStateEnded || [gesture state] == UIGestureRecognizerStateCancelled) {
// If we have scrolled over a 1/3 of the way to kDeleteViewWidth, move to kDeleteViewWidth
if (point.x < -(kDeleteViewWidth / 3)) {
[UIView animateWithDuration:kAnimationDuration animations:^{
[_topView setFrame:CGRectMake(-kDeleteViewWidth, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
}];
_isSwiped = YES;
}
// Otherwise animation back to 0
else {
[UIView animateWithDuration:kAnimationDuration animations:^{
[_topView setFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)];
} completion:^(BOOL finished) {
[self removeShadow];
}];
_isSwiped = NO;
}
}
}