NSArray元素无法匹配mapView.annotations循环中的Swift数组元素类型

时间:2015-06-12 13:59:11

标签: ios swift nsarray mkannotation

我想在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
}

在委托方法之后我找到了scale并希望在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
    }
}

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

1 个答案:

答案 0 :(得分:0)

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

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