如何继承UITextView:实现一个水平滑动

问题描述:

我试图在UITextView上实现一个水平滚动。我发现这解释here.如何继承UITextView:实现一个水平滑动

但是,我不明白我怎么可以'子类'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

请参阅[MPTextView](http://miphol.com/muse/2009/05/subclassing-uitextview.html)可能会给你一个提示。 – 2011-04-18 15:06:41

+0

@AhmadTK:换句话说,我只是做一个新的m和h文件,然后#import它到我想使用MPTextView的代码?感谢您的链接! – 2011-04-18 15:19:10

+0

只需将它们拖到您的项目和是进口** MPTextView.h ** – 2011-04-18 15:21:22

好吧,这是你做的。

当你想为一个对象创建子类时,你为它创建了.h和.m文件。

创建一个名为SwipeableTextView.h文件,并插入代码里:

#import <Foundation/Foundation.h> 

    @interface SwipeableTextView : UITextView { 

    } 

@end 

然后创建一个文件SwipeableTextView.m和inser此进去:

#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 

要使用这个新sublass您的项目执行以下操作。

导入头:

#import "SwipeableTextView.h" 

,然后不是创建的UITextView normaly你会喜欢这个创建SwipeableTextView:

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

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

+0

优秀的解释!我开始从AhmadTK提供的评论中猜测这一点,但这确实有帮助。非常感谢! – 2011-04-18 15:20:49

+0

Np。很高兴我能帮上忙。 – Cyprian 2011-04-18 15:23:56

+0

我成功实现了这一点。但是,我仍然无法看到我现在如何在UITextView中实现水平滚动。我是否需要在SwipeableTextView.m文件中实现此方法,还是需要在viewDelegate中执行此操作?我正在讨论修改 - (void)touchesBegan:(NSSet *)触及事件:(UIEvent *)事件{和 - (void)touchesMoved:(NSSet *)触及事件:(UIEvent *)event { – 2011-04-18 16:31:22