如何在应用程序中实现“手指跟踪”图层,如iOS模拟器

时间:2012-09-25 16:21:06

标签: ios uiview touch-event

现在我们的应用已发货,我们的培训负责人有一个要求:

“该应用程序很酷,但我们需要一种显示手指移动的模式,例如在模拟器或iPad的激光指针模式中如何显示手势[我们可以拍摄电影]。”

这是一个应用程序,它为每个选项卡使用带有xib的UITabBarController(4)。这是一个非常复杂的应用程序,具有交互式手势识别器,我正在做“第一次,不做任何伤害”的噩梦......

我尝试使用此处的方案在标签控制器上方添加一个简单的“全局UIView”:http://www.james-vandyne.com/creating-universal-overlays-using-uiviews-on-ios/

我的“PresentationView”尝试:

在我的app委托中:

UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow];
[self setPresentationView:[[PresentationView alloc] initWithFrame: [mainWindow frame]]];
[mainWindow insertSubview:presentationView aboveSubview:mainWindow];

班级:

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

@interface PresentationView : UIView
{
    NSMutableDictionary *touchesInView;
}
@property (nonatomic, retain) NSMutableDictionary *touchesInView;
@end


#import "PresentationView.h"
#import "TouchPoint.h"

@implementation PresentationView
@synthesize touchesInView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [ self setAlpha:0.1];
        [self setBackgroundColor:[UIColor whiteColor]];

        [self setUserInteractionEnabled:YES];
        [self setMultipleTouchEnabled:YES];
        touchesInView = [[NSMutableDictionary alloc] init];
    }
    return self;
}

- (void) dealloc
{
    [touchesInView release];
    touchesInView = nil;

    [super dealloc];
}

- (void)drawRect:(CGRect)rect
{
    if ([touchesInView count] < 1)
        return;

    CGContextRef context = UIGraphicsGetCurrentContext();
    NSEnumerator *touchEnum = [touchesInView objectEnumerator];
    TouchPoint *nextTouch;
    float rad = 24;

    while (nextTouch = [touchEnum nextObject]) {
        [[ UIColor redColor] set];
        CGContextAddArc(context, nextTouch.current.x, nextTouch.current.y, rad, 0.0, M_PI*2, 1);
        CGContextFillPath(context);
        CGContextStrokePath(context);
    }
}

- (UIView *) hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    [self setNeedsDisplay];

    return [[[[[UIApplication sharedApplication] keyWindow] subviews] objectAtIndex:0] hitTest:point withEvent:event];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    for( UITouch *t in touches )
    {
        NSValue *key = [NSValue valueWithPointer:t];
        CGPoint loc = [t locationInView:self];
        TouchPoint *newTouch = [[TouchPoint alloc] init];

        [newTouch setBegan:loc];
        [newTouch setCurrent:loc];
        [newTouch setTapCount:[t tapCount]];

        [touchesInView setObject:newTouch forKey:key];

        [newTouch release];
    }
    [self setNeedsDisplay];

    [[[[[UIApplication sharedApplication] keyWindow] subviews] objectAtIndex:0] touchesBegan:touches withEvent:event];
}
// More touch methods- all ignored

这种方式有效 - 视图位于所有其他视图的顶部,hitTest:withEvent:点火,我在drawRect:的生命点成功绘制了一个红色圆圈。

但是当我尝试跟踪触摸时它会崩溃 - touchesBegan/Moved/Ended不会触发。 (是的,我为此全局视图启用了UserInteractionEnabledsetMultipleTouchEnabled。幸运的是,我没有搞砸任何下面的手势识别 - 其他视图正在获取事件,并且应用程序在这种全局视图下表现正常。

通常我使用IB来构建/定位视图,而不是从头开始在代码中构建它们,所以很可能有(一个?/几个?)简单的UIView属性我没有正确设置......但是我看不到它。或者我是否会因此而陷入困境,试图将事件向前推进?

1 个答案:

答案 0 :(得分:1)

有一个名为FingerTips的好框架可以做到这一点。

它也很容易实现,如果有外部显示器,它只显示圆圈,所以你要呈现它。