ios - 如何在类似iOS模拟器的app中实现 "finger-tracing"层

标签 ios uiview touch-event

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

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

这是一个为每个选项卡 (4) 使用带有 xibs 的 UITabBarController 的应用程序。这是一个非常复杂的应用程序,带有交互手势识别器,我一直在做“首先,不要伤害”的噩梦......

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

我的“PresentationView”尝试:

在我的应用委托(delegate)中:

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

类(class):

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

这有点管用 - View 位于所有其他 View 之上,并且 hitTest:withEvent:开火,我成功地在 drawRect: 中的命中点绘制了一个红色圆圈.

但是当我尝试跟踪触摸时它崩溃了- touchesBegan/Moved/Ended不要开火。 (是的,我已经为这个全局 View 打开了 UserInteractionEnabledsetMultipleTouchEnabled)。幸运的是,我没有在下面的任何东西上搞砸手势识别——其他 View 正在获取事件,并且应用程序在这个全局 View 下运行正常。

通常我使用 IB 来构建/定位 View ,而不是从头开始在代码中构建它们,所以很可能有(一个?/几个?)简单的 UIView 属性我没有设置正确......但我看不到它。还是我会让自己陷入困境,试图将事件转发到链中?

最佳答案

有一个称为 FingerTips 的好框架可以执行此操作。

它也很容易实现,如果有外部显示器,它只会显示圆圈,所以你正在展示它。

关于ios - 如何在类似iOS模拟器的app中实现 "finger-tracing"层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12587079/

相关文章:

android - 如何在指定的持续时间后以正确的方式运行动画?

ios - 如何在 SpriteKit 中为 physicsWorld 设置 contactDelegate?

ios - 单点触控 : How to dismiss any open UIAlertViews

iphone - 从 uiview 获取图像

android - 处理触摸事件 - onInterceptTouchEvent 和 onTouchEvent

ios - 测试 iPhone View Controller : where to start?

ios - Refresh Control with Search Display Controller 出现在搜索栏上方

ios - 触摸方法未在放置在 UIScrollView 中的 UIView 上调用

ios - 多个 View 处理的触摸事件

android - 在android中同时触摸一次日历选择多个日期