当框架原点不为 0,0 时,iphone UIButton 选择器不起作用
我尝试了很多不同的方法,但找不到我的代码有什么问题,希望你能帮助我!
我正在制作一个自定义标注气泡,当单击地图上的图钉时显示,以显示气泡内的一些图像,并且我想要详细信息披露按钮,以便用户可以单击并访问更多信息。我的问题是显示了气泡和按钮,但单击按钮不会调用选择器方法。
//编辑:抱歉,因为我第一次放置了太多代码,我将编辑尝试更具体,感谢您的回答
在方法中:
- (id) initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier
首先,我显示一个图像视图来更改自定义图标而不是
self = [super initWithAnnotation:myAnnotation reuseIdentifier:reuseIdentifier];
self.frame = CGRectMake(0, 0, kWidth, kHeight);
self.backgroundColor = [UIColor clearColor];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];
imageView.frame = CGRectMake(kBorder, kBorder, kWidth - 2 * kBorder, kWidth - 2 * kBorder);
[self addSubview:imageView];
我添加的 普通引脚将其作为子视图,同时为了自定义气泡标注,我创建了一些标签和图像,并将其添加到视图 calloutView 中,该视图最初是隐藏的,直到调用 pin 的 select 方法
calloutView = [[UIView alloc] initWithFrame:CGRectMake(10, 30, restNameLbl.frame.size.width + 30, 60)];
calloutView.hidden = YES;
之后,我创建了圆角矩形背景并大小到文本,我忽略了这一点代码的一部分,因为我认为现在不重要,让我知道是否应该把它放在里面,我使用一些图形函数来模拟默认的iphone气泡图样式,
然后我将之前创建的所有元素添加到calloutView中:
[calloutView addSubview:backgroundRect];
[calloutView addSubview:restNameLbl];
[calloutView addSubview:likeImg];
[calloutView addSubview:likesLbl];
[calloutView addSubview:noLikeImg];
[calloutView addSubview:noLikesLbl];
然后我将该视图放在正确的位置
//position callout above pin
calloutView.frame = CGRectMake(-25, -calloutView.frame.size.height-10, calloutView.frame.size.width + 50, calloutView.frame.size.height);
[self addSubview:calloutView];
并在同一个气泡视图中 - 称为 calloutView 我想显示一个详细信息披露按钮,以便用户可以访问更多信息,但如果我将其直接添加到 calloutView ,则选择器事件永远不会触发。因此,我尝试以不同的方式执行此操作,即向父视图添加一个按钮,将其隐藏状态设置为 yes 并在选择器方法上显示它
accessory = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[accessory setTitle:myAnnotation.idRest forState:UIControlStateNormal];
accessory.hidden= YES;
//注意:如果我省略此框架,这就是问题行,一切都很完美,或者如果我将 origin.x 和 origin.y 设置为 0,0 但是当我更改该数字以将其显示在视图的正确位置时,按钮选择器停止工作
accessory.frame = CGRectMake(110, calloutView.frame.origin.y+25, accessory.frame.size.width, accessory.frame.size.height);
然后我添加单击时调用选择器事件,正如我所说的函数calloutAccessoryTap 仅当未指示配件框架或 origin.x 和 y 均设置为 0 时才调用它
accessory.userInteractionEnabled = YES;
[accessory addTarget:self action:@selector(calloutAccessoryTap) forControlEvents: UIControlEventTouchUpInside | UIControlEventTouchCancel];
最后我将按钮添加到主视图
[self addSubview:accessory];
所以最初视图只是一个图标,带有标签的标注视图并且图像被隐藏,详细信息按钮也会发生同样的情况,并且当选择图钉并且需要显示气泡时,会显示标注视图和按钮。一切都可以在屏幕上看到,但是当配件的框架设置到正确位置时 - 与原点 x 和 y 0,0 不同,选择器停止工作。
希望你能帮助我,因为我尝试了很多不同的方法,但没有一个起作用。
I've tried many many different ways but can't find what's wrong with my code, hope you can help me!
I'm making a custom callout bubble to be shown when clicking on a pin on a map, to show some images inside the bubble, and I want the detaildisclosure button so the user can click and access to more info. My problem is that the bubble and button are shown but the click on the button does not called the selector method.
//EDIT: sorry because the first time I put too much code, I'll edit trying to be more specific, thanks for your answers
In the method:
- (id) initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier
At first I show an imageview to change a custom icon instead of normal pin
self = [super initWithAnnotation:myAnnotation reuseIdentifier:reuseIdentifier];
self.frame = CGRectMake(0, 0, kWidth, kHeight);
self.backgroundColor = [UIColor clearColor];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon.png"]];
imageView.frame = CGRectMake(kBorder, kBorder, kWidth - 2 * kBorder, kWidth - 2 * kBorder);
[self addSubview:imageView];
I add it as a subview, and at the same time to customize the bubble callout I create some labels and images and I added to a view calloutView that it's hidden at first till the select method of the pin is called
calloutView = [[UIView alloc] initWithFrame:CGRectMake(10, 30, restNameLbl.frame.size.width + 30, 60)];
calloutView.hidden = YES;
After that I create rounded rect background and size to text, I leave out that part of code because I don't think it's important now, let me know if I should put it, I use some graphics functions to simulate the default iphone bubble map sytle
And I add all the elements created before to the calloutView:
[calloutView addSubview:backgroundRect];
[calloutView addSubview:restNameLbl];
[calloutView addSubview:likeImg];
[calloutView addSubview:likesLbl];
[calloutView addSubview:noLikeImg];
[calloutView addSubview:noLikesLbl];
And then I place that view at the right place
//position callout above pin
calloutView.frame = CGRectMake(-25, -calloutView.frame.size.height-10, calloutView.frame.size.width + 50, calloutView.frame.size.height);
[self addSubview:calloutView];
And in the same bubble view - called calloutView I want to show a detail disclosure button so that the user can access to more info, but if I added it directly to the calloutView the selector events are never trigger. So I tried to do it in a different way that it's adding a button to the parent view, put its hidden state to yes and show it on the selector method
accessory = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[accessory setTitle:myAnnotation.idRest forState:UIControlStateNormal];
accessory.hidden= YES;
//NOTE: here it's the problem if i leave out this frame line, everything works perfect or if i set the origin.x and origin.y to 0,0 but when i changed that number to show it at its right place of the view the button selector stops working
accessory.frame = CGRectMake(110, calloutView.frame.origin.y+25, accessory.frame.size.width, accessory.frame.size.height);
Then I add the selector event to called when click and as I said the function calloutAccessoryTap it's called only if the frame of accessory it's not indicated or if origin.x and y are both set to 0
accessory.userInteractionEnabled = YES;
[accessory addTarget:self action:@selector(calloutAccessoryTap) forControlEvents: UIControlEventTouchUpInside | UIControlEventTouchCancel];
And last I added the button to the main view
[self addSubview:accessory];
So at first the view is just an icon, the callout view with the labels and images is hidden, the same happen to the detail button and when the pin is selected and the bubble needs to be shown the callout view and the button are shown. Everything is seen right on the screen, but when the frame of the accessory is set to the good position - distinct to origin x and y 0,0 the selector stop working.
Hope you can help me, because I tried many different things but none of them are working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
浏览代码非常耗时,所以我只给你一个指导。您是否在
UIButton“accessory”
顶部添加子视图?如果是,那么该按钮当然不会检测到点击。子视图被添加到彼此的顶部,因此将透明视图放在按钮顶部将有效地使其变得无用。It's time consuming to go through the code so I'll just give you a pointer. Are you adding subViews on top of the
UIButton "accessory"
? If yes, then of course the button won't detect taps. subViews are added on top of each other so putting a transparent view on top of the button will effectively render it useless.