Successful Swipe in UITextView?

后端 未结 6 995
暖寄归人
暖寄归人 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:06

    An even better solution, I have found, is to simply pass touches back to the superview:

    @interface SwipeableTextView : UITextView {
    }
    
    @end
    
    @implementation SwipeableTextView
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesBegan:touches withEvent:event];
    
        [self.superview touchesBegan:touches withEvent:event];
    
    }
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesMoved:touches withEvent:event];
    
        [self.superview touchesMoved:touches withEvent:event];
    }
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesEnded:touches withEvent:event];
    
        [self.superview touchesEnded:touches withEvent:event];
    } 
    
    @end
    
    0 讨论(0)
  • 2020-12-10 09:08

    Here is a subclass of a UITextView that will detect a swipes gesture...

    Is this what you are looking for?

    #import <UIKit/UIKit.h>
    
    #define kMinimumGestureLength   25
    #define kMaximumVariance        5
    
    @interface SwipeableTextView : UITextView {
        CGPoint gestureStartPoint;
    }
    
    @end
    
    @implementation SwipeableTextView
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesBegan:touches withEvent:event];
    
        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);
    
        if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) {
            NSLog(@"Horizontal swipe detected");
        }
    
    }
    
    @end
    
    0 讨论(0)
  • 2020-12-10 09:18

    Add a transparent UIView on top of the UITextView and use it to handle the swipe, and send the touchesBegan/Moved/Ended/Canceled: messages to the text view to preserve normal interaction.

    0 讨论(0)
  • 2020-12-10 09:24

    You could also use a two finger swipe. When I get home I can elaborate with some code but that was what I have used

    0 讨论(0)
  • 2020-12-10 09:25

    Usually you implement the touch responding events in a UITableViewCell, which can recognize the swipe and do something. UITableViews inherit from UIScrollView which does not like having touch events overridden.

    0 讨论(0)
  • 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 <UIKit/UIKit.h>
    
    #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
    
    0 讨论(0)
提交回复
热议问题