观察捏多点触摸手势在一个UITableView
-
18-09-2019 - |
题
我期待实现合拢/缩小一个UITableView的顶部,我也考虑过多种方法,包括这一个:
不过,虽然我可以创建一个UIViewTouch
对象,它覆盖到我的UITableView,滚动事件不被转发到我的UITableView,我仍然可以选择单元格,并且它们通过适当的触发一个新的ViewController对象的转变作出反应。但我不能,尽管传递的touchesBegan,touchesMoved滚动的UITableView,并touchesEnded事件。
解决方案
这似乎是一个经典的问题。在我的情况下,我想在拦截一个UIWebView某些事件不能被继承,等等等等。
我发现,做的最好的办法是使用一个UIWindow拦截的事件:
EventInterceptWindow.h
@protocol EventInterceptWindowDelegate
- (BOOL)interceptEvent:(UIEvent *)event; // return YES if event handled
@end
@interface EventInterceptWindow : UIWindow {
// It would appear that using the variable name 'delegate' in any UI Kit
// subclass is a really bad idea because it can occlude the same name in a
// superclass and silently break things like autorotation.
id <EventInterceptWindowDelegate> eventInterceptDelegate;
}
@property(nonatomic, assign)
id <EventInterceptWindowDelegate> eventInterceptDelegate;
@end
EventInterceptWindow.m:
#import "EventInterceptWindow.h"
@implementation EventInterceptWindow
@synthesize eventInterceptDelegate;
- (void)sendEvent:(UIEvent *)event {
if ([eventInterceptDelegate interceptEvent:event] == NO)
[super sendEvent:event];
}
@end
创建类,改变类的一个UIWindow在你的MainWindow.xib到EventInterceptWindow,然后某处设置eventInterceptDelegate到要拦截事件视图控制器。实施例截取的双抽头:
- (BOOL)interceptEvent:(UIEvent *)event {
NSSet *touches = [event allTouches];
UITouch *oneTouch = [touches anyObject];
UIView *touchView = [oneTouch view];
// NSLog(@"tap count = %d", [oneTouch tapCount]);
// check for taps on the web view which really end up being dispatched to
// a scroll view
if (touchView && [touchView isDescendantOfView:webView]
&& touches && oneTouch.phase == UITouchPhaseBegan) {
if ([oneTouch tapCount] == 2) {
[self toggleScreenDecorations];
return YES;
}
}
return NO;
}
相关资讯: http://iphoneincubator.com/blog/windows-views/360idev -iPhone开发人员的会议呈递
其他提示
尼姆罗德写道:
某处eventInterceptDelegate设置为要拦截事件视图控制器
我没有立即明白这种说法。对于谁比谁有同样的问题,因为我的利益,我做的方式是通过添加如下代码到我UIView子类必须检测触摸。
- (void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Register to receive touch events
MyApplicationAppDelegate *appDelegate = (MyApplicationAppDelegate *) [[UIApplication sharedApplication] delegate];
EventInterceptWindow *window = (EventInterceptWindow *) appDelegate.window;
window.eventInterceptDelegate = self;
}
- (void) viewWillDisappear:(BOOL) animated
{
// Deregister from receiving touch events
MyApplicationAppDelegate *appDelegate = (MyApplicationAppDelegate *) [[UIApplication sharedApplication] delegate];
EventInterceptWindow *window = (EventInterceptWindow *) appDelegate.window;
window.eventInterceptDelegate = nil;
[super viewWillDisappear:animated];
}
- (BOOL) interceptEvent:(UIEvent *) event
{
NSLog(@"interceptEvent is being called...");
return NO;
}
,点击
这interceptEvent:
的版本是一个简单的实现双指缩放检测。 NB。某些代码被采取从由Apress出版从iPhone 3成长。
CGFloat initialDistance;
- (BOOL) interceptEvent:(UIEvent *) event
{
NSSet *touches = [event allTouches];
// Give up if user wasn't using two fingers
if([touches count] != 2) return NO;
UITouchPhase phase = ((UITouch *) [touches anyObject]).phase;
CGPoint firstPoint = [[[touches allObjects] objectAtIndex:0] locationInView:self.view];
CGPoint secondPoint = [[[touches allObjects] objectAtIndex:1] locationInView:self.view];
CGFloat deltaX = secondPoint.x - firstPoint.x;
CGFloat deltaY = secondPoint.y - firstPoint.y;
CGFloat distance = sqrt(deltaX*deltaX + deltaY*deltaY);
if(phase == UITouchPhaseBegan)
{
initialDistance = distance;
}
else if(phase == UITouchPhaseMoved)
{
CGFloat currentDistance = distance;
if(initialDistance == 0) initialDistance = currentDistance;
else if(currentDistance - initialDistance > kMinimumPinchDelta) NSLog(@"Zoom in");
else if(initialDistance - currentDistance > kMinimumPinchDelta) NSLog(@"Zoom out");
}
else if(phase == UITouchPhaseEnded)
{
initialDistance = 0;
}
return YES;
}
,点击
编辑:尽管此代码在iPhone模拟器的工作100%的罚款,当我跑了一个iPhone设备上我遇到的有关表格滚动奇怪的错误。如果这也发生在你身上,然后强制interceptEvent:
方法在所有情况下返回NO。这意味着超也将处理触摸事件,所幸这并没有打破我的代码。