博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Observing pinch multi-touch gestures in a UITableView
阅读量:6095 次
发布时间:2019-06-20

本文共 5367 字,大约阅读时间需要 17 分钟。

I am looking to implement a pinch in/out on top of a UITableView, I have looked at several methods including this one:

 

But while I can create a UIViewTouch object and overlay it onto my UITableView, scroll events are not being relayed to my UITableView, I can still select cells, and they respond properly by triggering a transition to a new ViewController object. But I can not scroll the UITableView despite passing the touchesBegan, touchesMoved, and touchesEnded events.

 

answer:

This seems to be a classic problem. In my case I wanted to intercept some events over a UIWebView which can't be subclassed, etc etc.

I've found that the best way to do it is to intercept the events using the 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 
eventInterceptDelegate;}@property(nonatomic, assign) id
eventInterceptDelegate;@end

EventInterceptWindow.m:

#import "EventInterceptWindow.h"@implementation EventInterceptWindow@synthesize eventInterceptDelegate;- (void)sendEvent:(UIEvent *)event {    if ([eventInterceptDelegate interceptEvent:event] == NO)        [super sendEvent:event];}@end

Create that class, change the class of your UIWindow in your MainWindow.xib to EventInterceptWindow, then somewhere set the eventInterceptDelegate to a view controller that you want to intercept events. Example that intercepts a double-tap:

- (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;}

Related info here: 

 

 

 

another:

 

#import 
#import "EventInterceptWindow.h"@interface ViewController : UIViewController
{ int initialDistance;}- (BOOL) interceptEvent:(UIEvent *) event;@end

 

- (void) viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];    // Register to receive touch events    AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];    EventInterceptWindow *window = (EventInterceptWindow *) appDelegate.window;    window.eventInterceptDelegate = self;}- (void) viewWillDisappear:(BOOL) animated{    // Deregister from receiving touch events    AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];    EventInterceptWindow *window = (EventInterceptWindow *) appDelegate.window;    window.eventInterceptDelegate = nil;    [super viewWillDisappear:animated];}- (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;}

 

#import 
#import "EventInterceptWindow.h"@class ViewController;@interface AppDelegate : UIResponder
{ EventInterceptWindow *_window; ViewController *_viewController;}@property (strong, nonatomic) EventInterceptWindow *window;@property (strong, nonatomic) ViewController *viewController;@end

 

 

#import "AppDelegate.h"#import "ViewController.h"@implementation AppDelegate@synthesize window = _window;@synthesize viewController = _viewController;- (void)dealloc{    [_window release];    [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    EventInterceptWindow *winTemp = [[EventInterceptWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];    self.window = winTemp;    [winTemp release];        ViewController *temp = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];    self.viewController = temp;    [temp release];        self.window.rootViewController = _viewController;    [self.window makeKeyAndVisible];    return YES;}

转载地址:http://lrgwa.baihongyu.com/

你可能感兴趣的文章
iOS知识小集·设置userAgent的那件小事
查看>>
移动端架构的几点思考
查看>>
Tomcat与Spring中的事件机制详解
查看>>
Spark综合使用及用户行为案例区域内热门商品统计分析实战-Spark商业应用实战...
查看>>
初学者自学前端须知
查看>>
Retrofit 源码剖析-深入
查看>>
Rust官方公布Rust1.0最新状态报告和最终时间表
查看>>
使用PHPWord对Word文件做模板替换
查看>>
Dubbo Mesh在闲鱼生产环境的落地实践
查看>>
Windows Server 2008 \u0026 2012在GCE上正式商用了
查看>>
The Agile Mind-Set作者访谈
查看>>
Xamarin发布支持64位的iOS/Mac统一API
查看>>
Firefox 38发布,支持DRM
查看>>
书评与访谈:Refactoring for Software Design Smells
查看>>
Spring框架5.1将提供对Java 11的支持
查看>>
敏捷方法在测试计划中的应用
查看>>
SpringOne 2017:与Pivotal聊大会、Spring、Reactor、WebFlux及其他
查看>>
Arduino 传感器: 控制28BYJ-48步进电机
查看>>
Scala在挖财的应用实践
查看>>
亚马逊推出自研芯片,英特尔地位受到威胁
查看>>