我需要通过滑动手势选择文字,而无需长按。似乎有两种方式:一种是继承UITextView
或对UITextView
做一些事情,另一种方法是使用核心文本制作新的用户界面零件。
我该怎么办?
答案 0 :(得分:1)
这是pan手势的另一个答案。 希望这是你想要的。
- (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;
}
}
答案 1 :(得分:0)
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类并覆盖这些事件可以得到你想要的东西。
希望它可以帮到你。
答案 2 :(得分:0)
就像巴利曼说的那样。
稍加修改。
首先将textView.text分隔为数组。
_swipeValue = [textView componentsSeparatedByString:@" "];
手势选择器中的Seconde
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 http://stackoverflow.com/questions/1708608/uitextview-selectedrange-not-displaying-when-set-programatically
textView.selectedRange = range;
有效。但如果你的textView.text有相同的单词,那就不完美了。
答案 3 :(得分:-1)
嗨也许是这样的
//
// 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