MKMapView响应touch事件

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px 'Lucida Grande'; color: #929292}

TapDetectingWindow.h

//
//  plist>
 
//  NBFisheryiPad
//
//  Created by shiqyn on 11-6-16.
//  Copyright 2011 Stongsoft. All rights reserved.
//

#import <Foundation/Foundation.h>


@protocol TapDetectingWindowDelegate<NSObject>
- (void)userDidTapObserveView:(id)tapPoint;
@end
@interface TapDetectingWindow : UIWindow {
    UIView *viewToObserve;
    id <TapDetectingWindowDelegate> controllerThatObserves;
}
@property (nonatomic, retain) UIView* viewToObserve;
@property (nonatomic, assign) id <TapDetectingWindowDelegate> controllerThatObserves;
@end

 

 TapDetectingWindow.m

//
//  plist>
 
//  NBFisheryiPad
//
//  Created by shiqyn on 11-6-16.
//  Copyright 2011 Stongsoft. All rights reserved.
//

#import "TapDetectingWindow.h"


@implementation TapDetectingWindow
@synthesize viewToObserve;
@synthesize controllerThatObserves;
- (id)initWithViewToObserver:(UIView *)view andDelegate:(id)delegate {
    if(self == [super init]) {
        self.viewToObserve = view;
        self.controllerThatObserves = delegate;
    }
    return self;
}
- (void)dealloc {
    [viewToObserve release];
    [super dealloc];
}
- (void)forwardTap:(id)touch 
{ 
    [controllerThatObserves userDidTapObserveView:touch];
}
- (void)sendEvent:(UIEvent *)event 
{
    [super sendEvent:event];
    if (viewToObserve == nil || controllerThatObserves == nil)
        return;
    NSSet *touches = [event allTouches];
    if (touches.count != 1)
        return;
    UITouch *touch = touches.anyObject;
    if (touch.phase != UITouchPhaseEnded)
        return;
    if ([touch.view isDescendantOfView:viewToObserve] == NO)
        return;
    CGPoint tapPoint = [touch locationInView:viewToObserve];
    
    NSArray *pointArray = [NSArray arrayWithObjects:[NSString stringWithFormat:@"%f", tapPoint.x],
						   [NSString stringWithFormat:@"%f", tapPoint.y], nil];
    if (touch.tapCount == 1) 
	{
        [self performSelector:@selector(forwardTap:)   withObject:pointArray afterDelay:0];
	}
	else if (touch.tapCount > 1)
	{
		[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(forwardTap:)   object:pointArray];
	}
}
@end


 

用法:

1.双击MainWindow.xib, 弹出的Window,按control+2,弹出的window attributes窗口中 将class identity设置成TapDetectingwindow.

 

2.在要使用TapDetectingwindow类中添加如下代码:

	TapDetectingWindow* tapDetectWindow = ((TapDetectingWindow *)[[UIApplication sharedApplication].windows objectAtIndex:0]);
	tapDetectWindow.viewToObserve = mapView;
	tapDetectWindow.controllerThatObserves = self;

  并实现delegate:

#pragma mark TapDetectingWindowDelegate
- (void)userDidTapObserveView:(id)tapPoint
{
	DLog(@">>>userDidTapObserveView");
	[self hideSearchRangeSlider];
}
 
 

你可能感兴趣的:(windows,F#)