全局检测触摸 [英] Detect touch globally

查看:102
本文介绍了全局检测触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何解决这个(相当)简单的问题,但我失败了,所以我真的需要你的建议。

I trying to figure out how to solve this (fairly) simple problem but I failing miserably, so I really need your advice.

我的应用程序包含一个uitabbar有几个标签。在其中一个我有一堆UIImageViews,每个UIImageViews代表一个图片的缩略图。同样,当您通过在应用程序图标上按下一秒钟从iPhone中删除应用程序时,我实现了一个UILongPressGestureRecognizer识别器,它开始摆动拇指。如果用户点击拇指角上出现的X,图片就会被移除。

My application consists of a uitabbar with several tabs. In one of them I have a bunch of UIImageViews each of which represents the thumbnail of a picture. Similarly as you remove apps from the iPhone by pressing for a second on the app icon, I implemented a UILongPressGestureRecognizer recognizer which starts wobbling the thumb. If the user taps on the 'X' that appears on the corner of the thumb the picture gets removed.

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

The logic that starts and stops the wobbling animation is inside a subclass of UIImageView that is used to show the thumb.

如果用户按下拇指以外的任何其他位置,我正在尝试取消摆动效果。理想情况下,如果可能的话,我宁愿将检测此取消触摸的代码放在UIImageView子类中。

What I'm trying to do is cancel the wobble effect if the user presses anywhere else outside the thumb. Ideally, if possible, I would prefer to place the code that detects this cancel touch inside the UIImageView subclass.

推荐答案

要抓住所有全局触摸事件我最终按如下方式对UIWindow进行子类化:

To catch all touch events globally I ended up subclassing UIWindow as follows:

//  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

然后每当我需要开始听所有触及全局我执行以下操作:

Then whenever I need to start listening to all touches globally I do the following:

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

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

在stopThumbnailWobble中我删除观察者并处理UITouch事件以决定是否删除拇指:

In stopThumbnailWobble I remove the observer and process the UITouch event to decide whether to remove the thumb or not:

- (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
    ...

希望这有助于其他人。

Hope this helps others.

这篇关于全局检测触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆