I'm trying to implement a horizontal scroll on an UITextView. I found this explained here.
However, I don't understand how I can 'subclass' a UITextView
. The code which is given and which I tried to implement is the following:
@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
Apparently, this is supposed to override a normal UITextView
which I can then call by referring to SwipeableTextView
(e.g. SwipeableTextView.text = @"Some Text";
). My question is, where do I put this piece of code? In my .m or .h file? I tried to put it beneath the implementation section of my m file, but this won't work as I already have an @interface
and @implementation
section. Any help would be very much appreciated.
EDIT: This works now:
//
// SwipeTextView.h
// Swipes
//
#import <Foundation/Foundation.h>
@interface SwipeTextView : UITextView {
CGPoint gestureStartPoint;
}
@property CGPoint gestureStartPoint;
@end
M File
//
// SwipeTextView.m
// Swipes
//
#import "SwipeTextView.h"
#define kMinimumGestureLength 10
#define kMaximumVariance 5
@implementation SwipeTextView
@synthesize gestureStartPoint;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch *touch =[touches anyObject];
gestureStartPoint = [touch locationInView:self.superview];
[self.superview touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
[self.superview touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self.superview];
CGFloat deltaXX = (gestureStartPoint.x - currentPosition.x); // positive = left, negative = right
//CGFloat deltaYY = (gestureStartPoint.y - currentPosition.y); // positive = up, negative = down
CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x); // will always be positive
CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y); // will always be positive
if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) {
if (deltaXX > 0) {
NSLog (@"Horizontal Left swipe detected");
}
else {
NSLog(@"Horizontal Right swipe detected");
}
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[self.superview touchesEnded:touches withEvent:event];
}
@end
And finally, this is how I create the subclass of this custom UITextView in my ViewController:
// UITextView
CGRect aFrame = CGRectMake(0, 100, 320, 200);
aSwipeTextView = [[SwipeTextView alloc] initWithFrame:aFrame];
aSwipeTextView.text = @"Some sample text. Some sample text. Some sample text.";
[self.view addSubview:aSwipeTextView];