Successful Swipe in UITextView?

后端 未结 6 1014
暖寄归人
暖寄归人 2020-12-10 08:45

Has anyone been able to implement a successful swipe gesture on a UITableView? By default this is not possible due to the scrolling nature of the control. I\'ve tried subcla

6条回答
  •  眼角桃花
    2020-12-10 09:28

    I took Ross's code and added a few things that helped me. In particular, this code won't respond to the swipe until it stops. It also prevents a swipe from reversing direction partway through.

    #import 
    
    #define kMinimumGestureLength   25
    #define kMaximumVariance        5
    
    typedef enum swipeDirection {
        kSwipeNone,
        kSwipeLeft,
        kSwipeRight
    } tSwipeDirection;
    
    @interface SwipeableTextView : UITextView {
        CGPoint gestureStartPoint;
        tSwipeDirection swipeDirection;
    }
    
    @end
    
    @implementation SwipeableTextView
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesBegan:touches withEvent:event];
    
        swipeDirection = kSwipeNone;
        UITouch *touch =[touches anyObject];
        gestureStartPoint = [touch locationInView:self];
    
    }
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesMoved:touches withEvent:event];
    
        UITouch *touch = [touches anyObject];
        CGPoint currentPosition = [touch locationInView:self];
    
        CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
        CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);
    
        // Check if we already started a swipe in a particular direction
        // Don't let the user reverse once they get going
        if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance &&
            swipeDirection == kSwipeNone) {
            if (gestureStartPoint.x < currentPosition.x) {
                swipeDirection = kSwipeRight;
            }
            else {
                swipeDirection = kSwipeLeft;
            }
        }
    }
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        if (swipeDirection == kSwipeRight) {
            NSLog(@"Swipe right");
        }
        else if (swipeDirection == kSwipeLeft) {
            NSLog(@"Swipe left");
        }
        [super touchesEnded:touches withEvent:event];
    } 
    
    @end
    

提交回复
热议问题