MKMapView + UITableView + 平滑滚动。为在 iOS 上构建交互式位置选择器提供框架。
此视图在需要显示可滚动、可选择的位置以及它们在地图上的位置时非常有用。可以轻松添加搜索栏以进行搜索或过滤。
将 LocationPickerView 文件夹拖放到您的应用程序中。
在您想要使用位置选择器的视图控制器中,将以下行添加到 viewDidLoad
LocationPickerView *locationPickerView = [[LocationPickerView alloc] initWithFrame:self.view.bounds];
locationPickerView.tableViewDataSource = self;
locationPickerView.tableViewDelegate = self;
[self.view addSubview:self.locationPickerView];
或者,您可以在 Storyboard 中设置 LocationPickerView
。只需设置视图的类为 LocationPickerView
并且确保连接 table view 的代理和数据源。
如果您想要更复杂,可以指定更多的选项
// Create the location picker
LocationPickerView *locationPickerView = [[LocationPickerView alloc] initWithFrame:self.view.bounds];
locationPickerView.tableViewDataSource = self;
locationPickerView.tableViewDelegate = self;
// Optional parameters
locationPickerView.delegate = self;
locationPickerView.shouldCreateHideMapButton = YES;
locationPickerView.pullToExpandMapEnabled = YES;
locationPickerView.defaultMapHeight = 190.0;
locationPickerView.parallaxScrollFactor = 0.4; // little slower than normal.
locationPickerView.backgroundViewColor = [UIColor yellowColor]; //set color to the tableView background without the map
// Optional setup
self.locationPickerView.mapViewDidLoadBlock = ^(LocationPickerView *locationPicker) {
locationPicker.mapView.mapType = MKMapTypeStandard;
};
self.locationPickerView.tableViewDidLoadBlock = ^(LocationPickerView *locationPicker) {
locationPicker.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
};
[self.view addSubview:self.locationPickerView];
特别有用的是 delegate
属性,它允许您知道何时发生重要的事情(例如,地图视图即将全屏展开)。
注意:不要设置 table view 的 backgroundColor
属性。请对单元格或 LocationPickerView
进行着色。
MIT 许可证 版权所有 © 2013 Christopher Constable