Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Successful Swipe in UITextView?

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 subclassing UITextView and implementing the swipe function in the instance methods, but no dice.

My UITextView has scrolling disabled - Unless there is another way to implement multiline text?

[Edit] What I should say is input multiline text[/Edit]

like image 292
mootymoots Avatar asked Dec 23 '22 06:12

mootymoots


2 Answers

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
like image 166
Ross Avatar answered Jan 06 '23 17:01

Ross


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
like image 21
Jason Avatar answered Jan 06 '23 17:01

Jason