2011-08-15 113 views
3

我想弄清楚如何解决这个(相当)简单的问题,但我失败了,所以我真的需要你的建议。检测全球触摸

我的应用程序由一个包含多个选项卡的uitabbar组成。在其中的一个中,我有一堆UIImageViews,每个都代表图片的缩略图。同样,当您通过按下应用程序图标一秒钟从iPhone中移除应用程序时,我实现了一个UILongPressGestureRecognizer识别器,该识别器开始抖动拇指。如果用户轻击拇指角上出现的“X”,图片就会被移除。

启动和停止摆动动画的逻辑位于用于显示拇指的UIImageView的子类中。

我想要做的是取消摇摆的影响,如果用户按下拇指外的任何地方。理想情况下,如果可能的话,我宁愿将检测到此取消触摸的代码置于UIImageView子类中。

回答

6

要赶上全球所有触摸事件,我结束了继承的UIWindow如下:

// CustomUIWindow.h 
#import <UIKit/UIKit.h> 

#define kTouchPhaseBeganCustomNotification @"TouchPhaseBeganCustomNotification" 

@interface CustomUIWindow : UIWindow 
@property (nonatomic, assign) BOOL enableTouchNotifications; 
@end 

// CustomUIWindow.m 
#import "CustomUIWindow.h" 

@implementation CustomUIWindow 

@synthesize enableTouchNotifications = enableTouchNotifications_; 

- (void)sendEvent:(UIEvent *)event 
{ 
    [super sendEvent:event]; // Apple says you must always call this! 

    if (self.enableTouchNotification) { 
     [[NSNotificationCenter defaultCenter] postNotificationName:kTouchPhaseBeganCustomNotification object:event]; 
    } 
}@end 

然后每当我需要开始听全触控全球我做以下:

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(stopThumbnailWobble:) 
              name:kTouchPhaseBeganCustomNotification 
              object:nil]; 

((CustomUIWindow *)self.window).enableTouchNotification = YES; 

在stopThumbnailWobble我删除观察者和PROC通过UITouch事件来决定是否删除拇指:

- (void)stopThumbnailWobble:(NSNotification *)event 
{  
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:kTouchPhaseBeganCustomNotification 
                object:nil]; 
    ((CustomUIWindow *)self.window).enableTouchNotification = NO; 

    UIEvent *touchEvent = event.object; 
    // process touchEvent and decide what to do 
    ... 

希望这有助于他人。

+0

我的应用程序崩溃给出此错误消息:[UIWindow setEnableTouchNotifications:]:无法识别的选择器发送到实例 –

+0

您是否继承了UIWindow? –

+0

不,我把它解决了,因为我的问题被其他方式解决..谢谢反正..至少我了解了一种新的东西,UIWindow是这样subclassed。 –

0

如果您必须在您的uiimageview子类中包含代码检测,那么我会告诉appdelegate已收到触摸以及在哪里。然后应用程序代表可以告诉你所有的uiimageviews,或者告诉viewcontroller它会告诉它是uiimageviews。

未经测试的代码:

appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate]; 
[appDelegate touchedAt:(int)xPos yPos:(int)yPos]; 
+0

其实我正在寻找某种通知观察,我可以从uiimageview代码激活来验证触摸是在uiimageview本身内部还是外部。 在uiimageview之外触摸会取消摆动效果。 –