我有一个应用程序,基本上是这样的。我有一个地图视图,在屏幕顶部有一个工具栏。当您按下该工具栏上的按钮时,您现在处于可以在地图上滑动手指的模式。滑动的起点和终点将表示矩形的角落。应用程序将绘制一个半透明的蓝色矩形覆盖层,以显示您选择的区域。当您放开手指时,矩形选择完成,并且应用程序开始搜索我的数据库中的位置。
我不处理圆形,但我认为您可以做类似的事情,其中您有两种选择模式(矩形或圆形)。在圆形选择模式下,滑动的起点和终点可以表示圆心和边缘(半径)。或者,直径线的两个端点。我会留下那一部分给你。
实现
首先,我定义了一个透明的覆盖层,用于处理选择(OverlaySelectionView.h):
#import <QuartzCore/QuartzCore.h>
#import <MapKit/MapKit.h>
@protocol OverlaySelectionViewDelegate
- (void) areaSelected: (CGRect)screenArea;
@end
@interface OverlaySelectionView : UIView {
@private
UIView* dragArea;
CGRect dragAreaBounds;
id<OverlaySelectionViewDelegate> delegate;
}
@property (nonatomic, assign) id<OverlaySelectionViewDelegate> delegate;
@end
以及OverlaySelectionView.m文件:
#import "OverlaySelectionView.h"
@interface OverlaySelectionView()
@property (nonatomic, retain) UIView* dragArea;
@end
@implementation OverlaySelectionView
@synthesize dragArea;
@synthesize delegate;
- (void) initialize {
dragAreaBounds = CGRectMake(0, 0, 0, 0);
self.userInteractionEnabled = YES;
self.multipleTouchEnabled = NO;
self.backgroundColor = [UIColor clearColor];
self.opaque = NO;
self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
}
- (id) initWithCoder: (NSCoder*) coder {
self = [super initWithCoder: coder];
if (self != nil) {
[self initialize];
}
return self;
}
- (id) initWithFrame: (CGRect) frame {
self = [super initWithFrame: frame];
if (self != nil) {
[self initialize];
}
return self;
}
- (void)drawRect:(CGRect)rect {
}
#pragma mark - Touch handling
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touch = [[event allTouches] anyObject];
dragAreaBounds.origin = [touch locationInView:self];
}
- (void)handleTouch:(UIEvent *)event {
UITouch* touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self];
dragAreaBounds.size.height = location.y - dragAreaBounds.origin.y;
dragAreaBounds.size.width = location.x - dragAreaBounds.origin.x;
if (self.dragArea == nil) {
UIView* area = [[UIView alloc] initWithFrame: dragAreaBounds];
area.backgroundColor = [UIColor blueColor];
area.opaque = NO;
area.alpha = 0.3f;
area.userInteractionEnabled = NO;
self.dragArea = area;
[self addSubview: self.dragArea];
[dragArea release];
} else {
self.dragArea.frame = dragAreaBounds;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self handleTouch: event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self handleTouch: event];
if (self.delegate != nil) {
[delegate areaSelected: dragAreaBounds];
}
[self initialize];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self initialize];
[self.dragArea removeFromSuperview];
self.dragArea = nil;
}
#pragma mark -
- (void) dealloc {
[dragArea release];
[super dealloc];
}
@end
然后我有一个类来实现上述定义的协议(MapViewController.h):
#import "OverlaySelectionView.h"
typedef struct {
CLLocationDegrees minLatitude;
CLLocationDegrees maxLatitude;
CLLocationDegrees minLongitude;
CLLocationDegrees maxLongitude;
} LocationBounds;
@interface MapViewController : UIViewController<MKMapViewDelegate, OverlaySelectionViewDelegate> {
LocationBounds searchBounds;
UIBarButtonItem* areaButton;
在我的 MapViewController.m 中,areaSelected
方法是我使用 convertPoint:toCoordinateFromView:
进行触摸坐标到地理坐标转换的地方:
#pragma mark - OverlaySelectionViewDelegate
- (void) areaSelected: (CGRect)screenArea
{
self.areaButton.style = UIBarButtonItemStyleBordered;
self.areaButton.title = @"Area";
CGPoint point = screenArea.origin;
point.y -= 44;
CLLocationCoordinate2D upperLeft = [mapView convertPoint: point toCoordinateFromView: mapView];
point.x += screenArea.size.width;
CLLocationCoordinate2D upperRight = [mapView convertPoint: point toCoordinateFromView: mapView];
point.x -= screenArea.size.width;
point.y += screenArea.size.height;
CLLocationCoordinate2D lowerLeft = [mapView convertPoint: point toCoordinateFromView: mapView];
point.x += screenArea.size.width;
CLLocationCoordinate2D lowerRight = [mapView convertPoint: point toCoordinateFromView: mapView];
searchBounds.minLatitude = MIN(lowerLeft.latitude, lowerRight.latitude);
searchBounds.minLongitude = MIN(upperLeft.longitude, lowerLeft.longitude);
searchBounds.maxLatitude = MAX(upperLeft.latitude, upperRight.latitude);
searchBounds.maxLongitude = MAX(upperRight.longitude, lowerRight.longitude);
[[self.view.subviews lastObject] removeFromSuperview];
[self performSelectorInBackground: @selector(lookupHistoryByArea) withObject: nil];
}
- (IBAction) selectArea: (id) sender
{
PoliteAlertView* message = [[PoliteAlertView alloc] initWithTitle: @"Information"
message: @"Select an area to search by dragging your finger across the map"
delegate: self
keyName: @"swipe_msg_read"
cancelButtonTitle: @"Ok"
otherButtonTitles: nil];
[message show];
[message release];
OverlaySelectionView* overlay = [[OverlaySelectionView alloc] initWithFrame: self.view.frame];
overlay.delegate = self;
[self.view addSubview: overlay];
[overlay release];
self.areaButton.style = UIBarButtonItemStyleDone;
self.areaButton.title = @"Swipe";
}
你会发现我的 MapViewController
有一个属性,areaButton
。这是工具栏上的一个按钮,通常显示为 Area。当用户按下它时,他们进入了区域选择模式,此时按钮标签更改为 Swipe 来提醒他们向右滑动(可能不是最好的UI,但这就是我所拥有的)。
另请注意,当用户按下 Area 进入区域选择模式时,我会向他们显示一个警告,告诉他们需要向右滑动。由于这可能只是提醒他们需要看到的一次,因此我使用了自己的PoliteAlertView,这是一个用户可以抑制(不再显示警告)的自定义 UIAlertView
。
我的 lookupHistoryByArea
只是一个方法,后台搜索我的数据库中的位置,并通过保存的 searchBounds
在地图上绘制新的覆盖物。对于你的应用程序,这显然会有所不同。
限制
由于这是让用户选择大概区域,我认为地理精度不是关键因素。在你的应用程序中似乎也不需要考虑这一点。因此,我只绘制带有90度角的矩形,不考虑地球曲率等因素。对于几英里范围内的区域,这应该可以胜任。
我必须对你的短语 touch based drawing 进行一些假设。我认为实现应用程序最简单、触摸屏用户使用最简单的方法是用一次单独的滑动定义区域。用触摸绘制矩形需要4次滑动,引入非闭合矩形的复杂性,产生松散的形状,可能无法获得用户想要的内容。所以,我试图保持UI的简洁。如果你确实想让用户在地图上绘图,请参见这个相关的答案。
此应用程序是在ARC之前编写的,并且没有针对ARC进行更改。
在我的应用程序中,我实际上为一些在主(UI)线程和后台(搜索)线程上访问的变量使用了互斥锁,我将这些代码从此示例中删除了。根据你的数据库搜索工作方式以及你选择运行搜索的方式(GCD等),你应该确保自己线程安全性的审计。