How to subclass a UITextView: Implement a horizontal swipe
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];
Ok this is what you do.
When you want to subclass an object you create .h and .m files for it so.
Create a file called SwipeableTextView.h and insert this code inside:
#import <Foundation/Foundation.h>
@interface SwipeableTextView : UITextView {
}
@end
Then create a file SwipeableTextView.m and inser this into it:
#import "SwipeableTextView.h"
@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
To use this new sublass in your project do the following.
import the header:
#import "SwipeableTextView.h"
and then instead creating the UITextView normaly you would create SwipeableTextView like this:
SwipeableTextView *sTextView = [[SwipeableTextView alloc] init];
That's it. Hope it helps.
精彩评论