如何子类化UITextView:实现水平滑动

时间:2011-04-18 15:02:19

标签: iphone objective-c cocoa-touch uitextview subclass

我正在尝试在UITextView上实现水平滚动。我发现这解释了here.

但是,我不明白我如何'继承'UITextView。给出的代码和我试图实现的代码如下:

@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

显然,这应该覆盖正常UITextView,然后我可以通过引用SwipeableTextView(例如SwipeableTextView.text = @"Some Text";)来调用它。我的问题是,我在哪里放这段代码?在我的.m或.h文件中?我试图将它放在我的m文件的实现部分下面,但这不起作用,因为我已经有@interface@implementation部分。非常感谢任何帮助。


编辑:现在有效:

//
//  SwipeTextView.h
//  Swipes
//

#import <Foundation/Foundation.h>

@interface SwipeTextView : UITextView {
    CGPoint     gestureStartPoint;
}

@property CGPoint gestureStartPoint;


@end

M档

//
//  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

最后,这就是我在ViewController中创建这个自定义UITextView的子类的方法:

// 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];

1 个答案:

答案 0 :(得分:5)

好的,这就是你做的。

如果要为对象创建子类,则为其创建.h和.m文件。

创建一个名为SwipeableTextView.h的文件,并将此代码插入其中:

#import <Foundation/Foundation.h>

    @interface SwipeableTextView : UITextView {

    }

@end

然后创建一个文件SwipeableTextView.m并将其插入其中:

#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

要在项目中使用此新子类,请执行以下操作。

导入标题:

#import "SwipeableTextView.h"

然后改为创建UITextView,你可以像这样创建SwipeableTextView:

SwipeableTextView *sTextView = [[SwipeableTextView alloc] init];

就是这样。 希望它有所帮助。