我想弄清楚在UITextView处理捏手势的最佳方式。目前我一直试图在UITextView中处理所有这些,但是我得到的结果并不一致。它似乎能够在触动开始的方法中捕捉到我的触动,但它并不总是在触动方法中被捕获。Inconsitant结果与在UITextView Pinches
在视图中处理触摸并在多点触控事件上使用UITextView传递会更好吗?将UITextView放置在滚动视图中做一些棘手的事情会更好吗?
在这一点上,我想要做的就是调整多点触控捏或扩大的字体大小,我可以工作,但它不是一致的,我想我已经设法混淆UITextView比实际得到的结果更多。
我控制的UITextView的子类,并实现UITextViewDelegate:
#import "MyUITextView.h"
@implementation MyUITextView
/* skipping unimportant code */
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if([touches count] == 2)
{
NSLog(@"two touches");
UITouch *first = [[touches allObjects] objectAtIndex:0];
UITouch *second = [[touches allObjects] objectAtIndex:1];
initialDistance = [self distanceBetweenTwoPoints:[first locationInView:self] toPoint:[second locationInView:self]];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touches moved");
if([touches count] == 2)
{
self.scrollEnabled = NO;
UITouch *first = [[touches allObjects] objectAtIndex:0];
UITouch *second = [[touches allObjects] objectAtIndex:1];
CGFloat currentDistance = [self distanceBetweenTwoPoints:[first locationInView:self] toPoint:[second locationInView:self]];
if(initialDistance == 0)
initialDistance = currentDistance;
else if(currentDistance > initialDistance)
{
NSLog(@"zoom in");
self.scrollEnabled = YES;
self.font = [UIFont fontWithName:[self.font fontName] size:[self.font pointSize] + 1.0f];
self.text = self.text;
}
else if(currentDistance < initialDistance)
{
NSLog(@"zoom out");
self.scrollEnabled = YES;
self.font = [UIFont fontWithName:[self.font fontName] size:[self.font pointSize] = 1.0f];
self.text = self.text;
}
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"touches ended.");
initialDistance = 0;
[super touchesEnded:touches withEvent:event];
}
-(CGFloat)distanceBetweenTwoPoints:(CGPoint)fromPoint toPoint:(CGPoint)toPoint
{
float x = toPoint.x - fromPoint.x;
float y = toPoint.y - fromPoint.y;
return sqrt(x*x + y*y);
}
-(BOOL)canBecomeFirstResponder
{ return NO; }
基本上我试图禁用滚动,当我在屏幕上有两个触摸,然后重新启用它,当我做。此外,禁用成为第一响应者的能力,这样我就不必与复制和粘贴菜单打架。如果有更好的方法来做到这一点,通过允许复制和粘贴菜单时使用一个单一的触摸我都耳朵。我想我基本上正在为我的第一次进入这个手势业务的更高级的例子工作。另外,由于控件处理所有的东西,我不认为它需要传递触摸事件,因为它处理它们自己。我错了吗?
最后,我的UITextView以编程方式创建并放置在UINavigationControl中。我不知道这是否有所作为。
说明:当你说它没有捕捉到touchesMoved中的触动时,你的意思是(1)方法不被调用(2)它被调用但不包含两个触摸或(3)它包含不相关的触摸? – TechZen 2009-12-15 20:09:12
根据我在调试器中的NSLog报告,它经常会在触摸中多次触发我的触摸,但只会显示Touches Moved的一些或两个日志报告,通常不会。所以,因为我的Log语句应该在调用该方法时触发,我相信它不会被调用。 此外我忘了一段代码。就在scrollEnabled行之前,有一个封装if语句将所有内容都包含在双重触摸中。我会解决这个问题。 – georryan 2009-12-15 21:37:37
你的手指不能同时移动。在这种情况下,将单独调用每个手指的touchesMoved,因此[touches count]将返回1.请记住,(NSSet *)触摸仅包含实际更新的触摸。 – 2009-12-15 21:46:43