gpt4 book ai didi

iphone - 将 UIButton 添加到 MKAnnotationView

转载 作者:行者123 更新时间:2023-11-29 13:22:08 28 4
gpt4 key购买 nike

我有一个 MKAnnotationView 的自定义类,我在其中尝试以循环方式向注释添加 8 个按钮。我已经添加了按钮。它们在 View 中的位置非常完美很好,但是没有调用按钮的操作。为什么没有调用操作,解决方案是什么。

-(void)setSelected:(BOOL)selected animated:(BOOL)animated`
{
[super setSelected:selected animated:animated];
if(selected)
{
UIView *circularView = [[UIView alloc]initWithFrame:CGRectMake(-55, -55, 110, 110)];
circularView.tag = 1000;

UIButton *btn1;
UIButton *btn2;
UIButton *btn3;
UIButton *btn4;
UIButton *btn5;
UIButton *btn6;
UIButton *btn7;
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[circularView addSubview:btn1];
btn1.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(buttonTapped:)];
tap.numberOfTapsRequired = 1;
[btn1 addGestureRecognizer:tap];

btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
[circularView addSubview:btn2];

btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
[circularView addSubview:btn3];

btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
[circularView addSubview:btn4];

btn5 = [UIButton buttonWithType:UIButtonTypeCustom];
[circularView addSubview:btn5];

btn6 = [UIButton buttonWithType:UIButtonTypeCustom];
[circularView addSubview:btn6];

btn7 = [UIButton buttonWithType:UIButtonTypeCustom];
[circularView addSubview:btn7];

[btn1 setBackgroundImage:[UIImage imageNamed:@"message-icon"] forState:UIControlStateNormal];
[btn2 setBackgroundImage:[UIImage imageNamed:@"message-icon"] forState:UIControlStateNormal];
[btn3 setBackgroundImage:[UIImage imageNamed:@"message-icon"] forState:UIControlStateNormal];
[btn4 setBackgroundImage:[UIImage imageNamed:@"message-icon"] forState:UIControlStateNormal];
[btn5 setBackgroundImage:[UIImage imageNamed:@"message-icon"] forState:UIControlStateNormal];
[btn6 setBackgroundImage:[UIImage imageNamed:@"message-icon"] forState:UIControlStateNormal];
[btn7 setBackgroundImage:[UIImage imageNamed:@"message-icon"] forState:UIControlStateNormal];

CGPoint point = CGPointMake(55, 55);
btn1.frame = CGRectMake(point.x - 15.00, point.y - 55.00, 30, 30);
btn2.frame = CGRectMake(point.x + 16.27, point.y - 39.94, 30, 30);
btn3.frame = CGRectMake(point.x + 24.00, point.y - 06.10, 30, 30);
btn4.frame = CGRectMake(point.x + 02.36, point.y + 21.04, 30, 30);
btn5.frame = CGRectMake(point.x - 32.36, point.y + 21.04, 30, 30);
btn6.frame = CGRectMake(point.x - 54.00, point.y - 06.10, 30, 30);
btn7.frame = CGRectMake(point.x - 46.27, point.y - 39.94, 30, 30);
[self addSubview:circularView];
}
else
{
[[self viewWithTag:1000] removeFromSuperview];
}
}
-(void)buttonTapped:(UITapGestureRecognizer*)sender
{

}

最佳答案

只需尝试像下面那样添加 UIButton 并使用 UITapGestureRecognizer 调用它的事件。请参见下面的示例...

- (MKAnnotationView *)mapView:(MKMapView *)mapView 
viewForAnnotation:(id<MKAnnotation>)annotation
{
MKAnnotationView *annView = (MKAnnotationView *)[mapView
dequeueReusableAnnotationViewWithIdentifier: @"pin"];
if (annView == nil)
{
annView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:@"pin"] autorelease];

annView.frame = CGRectMake(0, 0, 200, 50);

UIButton *pinButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
pinButton.frame = CGRectMake(0, 0, 140, 28);
pinButton.tag = 10;

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(btnPinTap:)];
tap.numberOfTapsRequired = 1;
[pinButton addGestureRecognizer:tap];
[tap release];

[annView addSubview:pinButton];
}

annView.annotation = annotation;

UIButton *pb = (UIButton *)[annView viewWithTag:10];
[pb setTitle:annotation.title forState:UIControlStateNormal];

return annView;
}

- (void) btnPinTap:(UITapGestureRecognizer *)gestureRecognizer
{
UIButton *btn = (UIButton *) gestureRecognizer.view;
MKAnnotationView *av = (MKAnnotationView *)[btn superview];
id<MKAnnotation> ann = av.annotation;
NSLog(@"btnPinTap: ann.title=%@", ann.title);
}

只需在 .h 文件中添加 Delegate ..

UIGestureRecognizerDelegate

只需将下面的方法放入您的 .m 文件中...

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
shouldRecognizeSimultaneouslyWithGestureRecognizer
:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}

希望对你有帮助...

关于iphone - 将 UIButton 添加到 MKAnnotationView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14121030/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com