我们如何确保注释 View 在 map 上按指定的顺序/位置放置?
我有一个情况,一个地址可以有多个注释(在彼此之上)。例如,Pin 编号 1、2、3 和 4 位于同一地理位置。加载 map 时,我希望 Pin # 1 位于顶部,但无法确保 Pin # 1 始终可见(在顶部)。
环顾四周,我找到了以下解决方案,但没有一个对我有用。
1).按照您想要的顺序将注释添加到 map 。
根据我的经验,这是行不通的!根据我的经验,使用 addAnnotations: 将注释添加在一起或将它们一个接一个地添加会产生相同的结果。注释 View 的顺序仍然是随机的。
[self.mapView addAnnotations:allAnnotations];
生成与以下相同的结果:
for (MyCustomAnnotation *annotation in allAnnotations) {
[self.mapView addAnnotation:annotation];
}
2).使用 bringSubviewToFront 或 sendSubviewToBack 指定所有注释的顺序。
根据我的经验,这是行不通的!例如,以下代码不会生成所需的结果:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
// .tag associated with each view specifies the order/hierarchy of annotation views
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"tag" ascending:YES];
NSArray *orderedAnnotationViews = [views sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
for (MKAnnotationView *annotationView in orderedAnnotationViews) {
[mapView sendSubviewToBack:annotationView];
}
}
3).使用 annotationView.layer.zPosition 指定所有注释的确切顺序。
考虑 4 个引脚重叠的场景,例如Pin 1、2、3 和 4。此方法的问题在于,点击特定注释 pin 或以编程方式选择它不会将其置于最前面。 zPosition 是绝对的。您可以在每次选择图钉时更改它,但这并不理想。
The zPosition property specifies the z-axis component of the layer's position. The zPosition is intended to be used to set the visual position of the layer relative to its sibling layers. It should not be used to specify the order of layer siblings, instead reorder the layer in the sublayer array.
想法?
最佳答案
我最终使用了 zPosition:
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"tag" ascending:NO];
NSArray *sortedViews = [views sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
// Set zPosition
for (MKAnnotationView *annotationView in sortedViews) {
annotationView.layer.zPosition = ++_zPositionForAnnotationViews;
}
// Do whatever
// . . .
}
- (void)mapView:(MKMapView *)aMapView didSelectAnnotationView:(MKAnnotationView *)view {
NSLog(@"The annotation tapped is: %@", view.annotation.title);
// Ignore tap on user location
if ([view.annotation isKindOfClass:[MKUserLocation class]]) {
return;
}
// Update zPosition (to bring it to front)
view.layer.zPosition = ++_zPositionForAnnotationViews;
// Do whatever
// . . .
}
其中 _zPositionForAnnotationViews 是类变量。
关于ios - 处理重叠的 MKAnnotationView(s),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27656090/
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上
我对图像处理完全陌生。我对JPEG内部是什么以及它是如何工作一无所知。我想知道,是否可以在某处找到执行以下简单操作的ruby代码:打开jpeg文件。遍历每个像素并将其颜色设置为fx绿色。将结果写入另一个文件。我对如何使用ruby-vips库实现这一点特别感兴趣https://github.com/ender672/ruby-vips我的目标-学习如何使用ruby-vips执行基本的图像处理操作(Gamma校正、亮度、色调……)任何指向比“helloworld”更复杂的工作示例的链接——比如ruby-vips的github页面上的链接,我们将不胜感激!如果有ruby-
我有一个super简单的脚本,它几乎包含了FayeWebSocketGitHub页面上用于处理关闭连接的内容:ws=Faye::WebSocket::Client.new(url,nil,:headers=>headers)ws.on:opendo|event|p[:open]#sendpingcommand#sendtestcommand#ws.send({command:'test'}.to_json)endws.on:messagedo|event|#hereistheentrypointfordatacomingfromtheserver.pJSON.parse(event.d
我想找到给定字符串中的所有匹配项,包括重叠匹配项。我怎样才能实现它?#Example"a-b-c-d".???(/\w-\w/)#=>["a-b","b-c","c-d"]expected#Solutionwithoutoverlappedresults"a-b-c-d".scan(/\w-\w/)#=>["a-b","c-d"],but"b-c"ismissing 最佳答案 在积极的前瞻中使用捕获:"a-b-c-d".scan(/(?=(\w-\w))/).flatten#=>["a-b","b-c","c-d"]参见Rubyde
我想查找字符串的结尾是否与单独字符串的开头重叠。例如,如果我有这两个字符串:string_1='Peoplesaynothingisimpossible,butI'string_2='butIdonothingeveryday.'如何找到string_1末尾的“butI”部分与string_2开头相同?我可以编写一个方法来遍历这两个字符串,但我希望得到一个包含我错过的Ruby字符串方法或Ruby习惯用法的答案。 最佳答案 将MARKER设置为一些从未出现在您的string_1和string_2中的字符串。有一些方法可以动态地做到这一
我正在尝试解析网页,但有时会收到404错误。这是我用来获取网页的代码:result=Net::HTTP::getURI.parse(URI.escape(url))如何测试result是否为404错误代码? 最佳答案 像这样重写你的代码:uri=URI.parse(url)result=Net::HTTP.start(uri.host,uri.port){|http|http.get(uri.path)}putsresult.codeputsresult.body这将打印状态码和正文。