我在IOS上创建地图,并在每个注释上点击我添加如下自定义子视图:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
//load pin callout
nearbyMapCallout *calloutView = [[nearbyMapCallout alloc]init];
calloutView = (nearbyMapCallout *)[[[NSBundle mainBundle] loadNibNamed:@"nearbyMapCallout" owner:self options:nil] objectAtIndex:0];
CGRect calloutViewFrame = calloutView.frame;
calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width/2 + 37, -calloutViewFrame.size.height + 35);
calloutView.frame = calloutViewFrame;
[view addSubview:calloutView];
}
那么现在如何在单击特定注释标注子视图时触发事件?
答案 0 :(得分:1)
你可以试试这个
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
if(![view.annotation isKindOfClass:[MKUserLocation class]]) {
CGSize calloutSize = CGSizeMake(100.0, 80.0);
UIView *calloutView = [[UIView alloc] initWithFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y-calloutSize.height, calloutSize.width, calloutSize.height)];
calloutView.backgroundColor = [UIColor whiteColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(5.0, 5.0, calloutSize.width - 10.0, calloutSize.height - 10.0);
[button setTitle:@"OK" forState:UIControlStateNormal];
[button addTarget:self action:@selector(checkin) forControlEvents:UIControlEventTouchUpInside];
[calloutView addSubview:button];
[view.superview addSubview:calloutView];
}
}
答案 1 :(得分:0)
添加时,您可以使用UITapGestureRecognizer
捕获视图的点击事件:
UITapGestureRecognizer *singleTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSingleTap:)];
[calloutView addGestureRecognizer:singleTap];
[singleTap release];
以下是调用calloutView:
时调用的方法- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
CGPoint location = [recognizer locationInView:[recognizer.view superview]];
//Add your code here..
}
希望这有帮助。
答案 2 :(得分:0)