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

发布于 2024-11-02 08:52:41 字数 3370 浏览 5 评论 0原文

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

但是,我不明白如何“子类化”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

显然,这应该覆盖普通的 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];

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雄赳赳气昂昂 2024-11-09 08:52:41

好吧,这就是你要做的。

当您想要子类化一个对象时,您可以为其创建 .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];

就是这样。
希望有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文