需要继承:
UIGestureRecognizerDelegate
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveView:)];
panGesture.maximumNumberOfTouches = 1;
panGesture.minimumNumberOfTouches = 1;
panGesture.cancelsTouchesInView = YES;
panGesture.delegate = self;
[self addGestureRecognizer:panGesture];
[panGesture release];
- (void)moveView:(UIPanGestureRecognizer *)gestureRecognizer
{
UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gestureRecognizer;
if (pan.state == UIGestureRecognizerStateBegan) {
CGRect rect = self.frame;
rect.origin.x = 0;
self.frame = rect;
}
else if (pan.state == UIGestureRecognizerStateChanged) {
CGPoint point = [pan translationInView:self];
// CGRect frame = _screenshotBView.frame;
CGRect frame = self.frame;
frame.origin.x = point.x > 0 ? point.x : 0;
self.frame = frame;
}
else if (pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateCancelled) {
BOOL x = self.frame.origin.x > self.frame.size.width * kBoundaryWidthRatio;
[UIView animateWithDuration:kPushAnimationDuration animations:^{
CGFloat left = x ? self.frame.size.width : 0;
CGRect rect = self.frame;
rect.origin.x = left;
self.frame = rect;
} completion:^(BOOL finished) {
if (x) {
[self removeFromSuperview];
}
}];
}
}