检测UIImagePicker实时视图触摸而不“抓住”它们

时间:2013-04-20 15:49:59

标签: ios uiimagepickercontroller

我正在编写一个允许用户使用UIImagePickerController拍照的应用程序。

我已经使用我自己的按钮栏定制了UIImagePicker,并在imagePicker视图上添加了一些其他按钮/选项(视图)。下面是相关代码:

self.photoPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.photoPicker.allowsEditing = NO;
self.photoPicker.showsCameraControls = NO;

[self.photoPicker.view addSubview:topButtonView];
[self.photoPicker.view addSubview:topButtonView2];

除了一个问题外,一切都按预期工作。我开始针对iOS 4及更高版本定位此应用程序,我需要具备的一个功能是嵌入在UIImagePickerController中的点击式聚焦功能。这是问题所在。在iOS 4上,当用户点击实时视图时,获取(由拾取器控制器自动显示)一个可视指示器(方形),显示该点是相机正在聚焦并调整曝光。在iOS 5和6上,视觉指示器消失了。功能(tap-to-foucus)仍然存在,但没有更多的方形显示用户设置焦点的位置。

我已经搜索并阅读了几个类似的问题(但并不完全),答案通常指向在UIImagePicker实时视图上添加透明子视图并捕获触摸事件。这很容易,但问题是,在我捕获用户触摸后,我发现无法将该触摸转发到UIImagePicker liveView。

我尝试创建一个自定义视图类来添加de imagepicker视图但是无法使其工作。这是自定义类代码:

@interface touchGrabberViewController : UIViewController 
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

{
CGPoint tapPoint;
__unsafe_unretained IBOutlet UIImageView *tapFocusIndicator;
NSTimer *indicatorDismisser;

}

@property (unsafe_unretained, nonatomic) UIImagePickerController *photoPicker;

@end



@implementation touchGrabberViewController

@synthesize photoPicker;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    tapFocusIndicator.hidden = YES;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    return photoPicker.view;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = touches.anyObject;

   tapPoint = [touch locationInView:self.view];
    [self showTapFocusIndicator];

    [super touchesBegan:touches withEvent:event];
}


- (void)showTapFocusIndicator
{

    if (!indicatorDismisser)
    {
        tapFocusIndicator.center = tapPoint;

        tapFocusIndicator.hidden = NO;

        indicatorDismisser = [NSTimer scheduledTimerWithTimeInterval:0.5
                                                           target:self
                                                         selector:@selector(hideTapFocusIndicatorWithTimer:)
                                                         userInfo:nil
                                                          repeats:NO];
    }

}

- (void)hideTapFocusIndicatorWithTimer:(NSTimer *)timer
{
    tapFocusIndicator.alpha = 1.0;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelay:0.5];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(resetTapFocusIndicator)];
    tapFocusIndicator.alpha = 0.0;

    [UIView commitAnimations];

    [indicatorDismisser invalidate];
    indicatorDismisser = nil;
}

- (void)resetTapFocusIndicator
{
    tapFocusIndicator.hidden = YES;
    tapFocusIndicator.alpha = 1.0;
}

通过这个课程,我得到一个非常相似的视觉指示器,用户点击显示,但用户触摸从未被转发回实时视图,因此焦点功能丢失。

最近我决定放弃iOS 4,只针对版本5和6,但我想念那个视觉指示器。作为相机应用程序用户,该指标非常有用。

我已经尝试用(UIView *)hitTest:(CGPoint)指向withEvent:(UIEvent *)事件的替代品,但没有运气。

任何人都知道如何实现这个目标,或者我做错了什么?

谢谢。

0 个答案:

没有答案