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

n.e*_*ind 2 iphone cocoa-touch subclass objective-c uitextview

我正在尝试在UITextView上实现水平滚动.我在这里解释了这个.

但是,我不明白我怎样才能'继承'a 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
Run Code Online (Sandbox Code Playgroud)

显然,这应该覆盖一个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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

最后,这是我在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];
Run Code Online (Sandbox Code Playgroud)

Cyp*_*ian 5

好的,这就是你做的.

当你想要对一个对象进行子类化时,你就可以为它创建.h和.m文件.

创建一个名为SwipeableTextView.h的文件并在其中插入以下代码:

#import <Foundation/Foundation.h>

    @interface SwipeableTextView : UITextView {

    }

@end
Run Code Online (Sandbox Code Playgroud)

然后创建一个文件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
Run Code Online (Sandbox Code Playgroud)

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

导入标题:

#import "SwipeableTextView.h"
Run Code Online (Sandbox Code Playgroud)

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

SwipeableTextView *sTextView = [[SwipeableTextView alloc] init];
Run Code Online (Sandbox Code Playgroud)

而已.希望能帮助到你.