`
zani
  • 浏览: 357679 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

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];
}
 
 
分享到:
评论

相关推荐

    (0010)-iOS/iPhone/iPAD/iPod源代码-地图(Map)-Map Callouts

    button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside) calloutView.addSubview(button) ``` 2. 为按钮添加响应方法: ```swift @objc func buttonTapped(sender: UIButton) { print(...

    swift-iOS仿链家地图找房-高德地图找房-需要自行用pod集成地图api

    11. **用户交互**:用户可能会通过滑动、缩放地图来寻找房源,因此需要处理地图的拖动、缩放手势,以及点击标注后的响应事件。 12. **数据持久化**:如果应用需要存储大量的房源信息,可以考虑使用CoreData或SQLite...

    Beginning_iPhone_3_Development代码

    10. **手势识别与响应**: 如UITapGestureRecognizer、UIPanGestureRecognizer等,实现用户界面的触摸事件处理。 11. **地图服务和定位API**: 如使用MKMapView集成地图功能,以及CLLocationManager获取设备位置信息...

    MapTestApp:Fisler先生的xcodeswift示例地图应用程序

    7. **用户交互**:为了响应用户的触摸事件,例如点击地图上的标记,MapTestApp会使用手势识别器和委托方法来实现交互逻辑。 8. **Storyboard和Auto Layout**:Xcode中的Storyboard文件用于设计和布局UI界面,而Auto...

    Beginning iOS 10 with Swift

    - **单元格选择**:响应用户的触摸事件,实现单元格选择功能。 - **单元格删除**:通过Swipe-to-delete手势来删除单元格。 - **UIAlertController**:创建和显示各种类型的对话框。 #### 十一、导航控制器与Segue ...

    伦斯勒班车(Rensselaer-Shuttle):伦斯勒校园班车追踪器

    伦斯勒班车(Rensselaer-Shuttle)是一款专为iPhone、iPad、iPod touch、Apple Watch以及Mac用户设计的应用程序,旨在方便用户实时追踪伦斯勒理工学院(Rensselaer Polytechnic Institute)的校园班车动态。...

Global site tag (gtag.js) - Google Analytics