iOS:滑动至 select 文本,无需长按

iOS: Swipe to select text without long press

我需要 select 通过 滑动手势 发送文本,而无需长按。似乎有两种方法:一种是继承 UITextView 或对 UITextView 做一些事情,另一种是使用 Core Text 来创建一个新的 UI 组件。

我该怎么办?

嗨,也许是这样的

//
//  ViewController.m
//  SwipeTextFieldDemo
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *swipeTextField;
@property (strong, nonatomic) NSMutableArray *swipeValues;
@end

int swipeIndex = 0;

@implementation ViewController
@synthesize swipeTextField = _swipeTextField;
@synthesize swipeValues = _swipeValues;

// Create list of colors
- (NSMutableArray *)swipeValues
{
    if(_swipeValues == nil) {
        _swipeValues = [[NSMutableArray alloc]initWithObjects:@"Red",
                                                              @"Blue",
                                                              @"Green",
                                                              @"Yellow",
                                                              @"Orange",
                                                              @"Pink",
                                                              @"White",
                                                              @"Black",
                                                              nil];
    }
    return _swipeValues;
}

- (void)swipeColor:(UISwipeGestureRecognizer *)gestureReconize
{   
    if(gestureReconize.direction == UISwipeGestureRecognizerDirectionLeft) {
        swipeIndex++;
        swipeIndex = (swipeIndex >= [self.swipeValues count]) ? 0 : swipeIndex;
    } else {
        swipeIndex--;
        swipeIndex = (swipeIndex < 0) ? [self.swipeValues count] - 1 : swipeIndex;
    }
    self.swipeTextField.text = [self.swipeValues objectAtIndex:swipeIndex];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Right swipe
    UISwipeGestureRecognizer *swr = [[UISwipeGestureRecognizer alloc] 
                                initWithTarget:self action:@selector(swipeColor:)];
    [swr setNumberOfTouchesRequired:1];
    [swr setDirection:UISwipeGestureRecognizerDirectionRight];
    [self.swipeTextField addGestureRecognizer:swr];

    // Left swipe
    UISwipeGestureRecognizer *swl = [[UISwipeGestureRecognizer alloc] 
                                initWithTarget:self action:@selector(swipeColor:)];
    [swl setNumberOfTouchesRequired:1];
    [swl setDirection:UISwipeGestureRecognizerDirectionLeft];
    [self.swipeTextField addGestureRecognizer:swl];

    self.swipeTextField.text = [self.swipeValues objectAtIndex:swipeIndex];
}

- (void)viewDidUnload
{
    [self setSwipeTextField:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end



//
//  ViewController.h
//  SwipeTextFieldDemo
//
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextFieldDelegate, UIGestureRecognizerDelegate>

- (void)swipeColor:(UISwipeGestureRecognizer *)gestureReconize;

@end

UIResponder 包含

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

制作自定义 UITextView class 并覆盖这些事件可以获得您想要的。

希望对您有所帮助。

就像baliman说的

稍作修改。

先把textView.text分开成数组

_swipeValue = [textView componentsSeparatedByString:@" "];

手势选择器中的第二个

if(gestureReconize.direction == UISwipeGestureRecognizerDirectionLeft) {
    swipeIndex++;
    swipeIndex = (swipeIndex >= [self.swipeValues count]) ? 0 : swipeIndex;
} else {
    swipeIndex--;
    swipeIndex = (swipeIndex < 0) ? [self.swipeValues count] - 1 : swipeIndex;
}
NSString *selectedValue = [self.swipeValues objectAtIndex:swipeIndex];

第三次将selectedValue转换为textView.text

的范围
NSRange range = [textView.text rangeOfString:selectedValue];

最终集textView.selectedRange

[textView select:self]; //See 
textView.selectedRange = range;

有效。但如果你的 textView.text 有相同的词,那就不完美了。

这是另一个带有平移手势的答案。 希望这就是你想要的。

- (IBAction)pan:(UIPanGestureRecognizer *)ges
{
    CGPoint point = [ges locationInView:ges.view];
    if (ges.state == UIGestureRecognizerStateBegan)
    {
        startPoint = point;
    }
    else if (ges.state == UIGestureRecognizerStateChanged || ges.state == UIGestureRecognizerStateEnded)
    {
        UITextPosition *start = [self.textView closestPositionToPoint:startPoint];
        UITextPosition *end = [self.textView closestPositionToPoint:point];
        UITextRange *range = [self.textView textRangeFromPosition:start toPosition:end];
        [self.textView select:self.textView];
        self.textView.selectedTextRange = range;
    }
}