【问题标题】:NSArray element failed to match the Swift Array Element type in mapView.annotations loopNSArray 元素未能匹配 mapView.annotations 循环中的 Swift Array 元素类型
【发布时间】:2015-06-12 13:59:11
【问题描述】:

我想在 mapView 中缩放 PinImages(当它缩小或缩小时),为此我这样做:

func resizePin(scale: CGFloat, image: UIImage) -> UIImage {

    var newSize = CGSizeMake(image.size.width * scale, image.size.height * scale)
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
    image.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))
    var newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage
}

在委托方法之后,我找到了比例,并想在 mapView 注释的循环中使用 resizePin 方法来调整它们的大小

func mapView(mapView: MKMapView!, regionWillChangeAnimated animated: Bool) {
    oldZoomLatitude = mapView.region.span.latitudeDelta
    oldZoomLongitude = mapView.region.span.longitudeDelta

}

func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) {
    newZoomLatitude = mapView.region.span.latitudeDelta
    newZoomLongitude = mapView.region.span.longitudeDelta

    mapScale = (newZoomLongitude! / oldZoomLongitude!)
    println(mapScale)

    for annotation in mapView.annotations as [MKAnnotationView]  { // EXCEPTION - fatal error: NSArray element failed to match the Swift Array Element type
    }
}

不知道如何使它正确,以及异常的原因是什么。有什么想法吗?

【问题讨论】:

    标签: ios swift nsarray mkannotation


    【解决方案1】:

    mapView.annotations 属性的类型为 MKAnnotation。如果您想修改每个注释的视图,请尝试以下代码:

    for annotation in mapView.annotations {
        let view = mapView.viewForAnnotation(annotation)
        // modify the view.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-07
      • 2015-09-27
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多