理解函数算法
Understanding function algorithm
在学习教程中,我使用函数显示可见矩形,包含所有注释(在 mapView 上),如图所示:
// 1
let rectToDisplay = self.treasures.reduce(MKMapRectNull) { (mapRect: MKMapRect, treasure: Treasure) -> MKMapRect in
// 2
let treasurePointRect =
MKMapRect(origin: treasure.location.mapPoint,
size: MKMapSize(width: 0, height: 0))
return MKMapRectUnion(mapRect, treasurePointRect)
// 3
}
// 4
self.mapView.setVisibleMapRect(rectToDisplay, edgePadding: UIEdgeInsetsMake(74, 10, 10, 10), animated: false)
一切正常,但我不明白它究竟是如何工作的。
在第三车道,我们正在做以下事情:
return MKMapRectUnion(mapRect, treasurePointRect)
在此之前,我们声明
mapRect: MKMapRect
因此,mapRect 没有任何初始值,并且假设不包含任何值。我对吗?
如果 mapRect 的值为零,MKMapRectUnion 究竟是如何计算的?有什么方法可以使用某种 NSLog 语句查看函数的每一步吗?
如果您好心,请详细解释一下该功能的工作原理。据我了解,函数尝试使 map Rect 结合 "zero" 值地图点和其他 mapPoint,具有正确的值。
您已经正确理解了所有内容。至于你的问题
How exactly MKMapRectUnion calculated, if mapRect have zero values?
苹果文档说:
If either rectangle is null, this method returns the other rectangle.
The origin point of the returned rectangle is set to the smaller of
the x and y values for the two rectangles. Similarly, the size and
width of the rectangle are computed by taking the maximum x and y
values and subtracting the x and y values for the new origin point.
具体是怎么计算的,你可以问苹果工程师或者对mapkit进行逆向工程。如果该功能确实可以正常工作,那么您就不必花那么多时间来偏离您的实际任务。
在学习教程中,我使用函数显示可见矩形,包含所有注释(在 mapView 上),如图所示:
// 1
let rectToDisplay = self.treasures.reduce(MKMapRectNull) { (mapRect: MKMapRect, treasure: Treasure) -> MKMapRect in
// 2
let treasurePointRect =
MKMapRect(origin: treasure.location.mapPoint,
size: MKMapSize(width: 0, height: 0))
return MKMapRectUnion(mapRect, treasurePointRect)
// 3
}
// 4
self.mapView.setVisibleMapRect(rectToDisplay, edgePadding: UIEdgeInsetsMake(74, 10, 10, 10), animated: false)
一切正常,但我不明白它究竟是如何工作的。
在第三车道,我们正在做以下事情:
return MKMapRectUnion(mapRect, treasurePointRect)
在此之前,我们声明
mapRect: MKMapRect
因此,mapRect 没有任何初始值,并且假设不包含任何值。我对吗?
如果 mapRect 的值为零,MKMapRectUnion 究竟是如何计算的?有什么方法可以使用某种 NSLog 语句查看函数的每一步吗?
如果您好心,请详细解释一下该功能的工作原理。据我了解,函数尝试使 map Rect 结合 "zero" 值地图点和其他 mapPoint,具有正确的值。
您已经正确理解了所有内容。至于你的问题
How exactly MKMapRectUnion calculated, if mapRect have zero values?
苹果文档说:
If either rectangle is null, this method returns the other rectangle. The origin point of the returned rectangle is set to the smaller of the x and y values for the two rectangles. Similarly, the size and width of the rectangle are computed by taking the maximum x and y values and subtracting the x and y values for the new origin point.
具体是怎么计算的,你可以问苹果工程师或者对mapkit进行逆向工程。如果该功能确实可以正常工作,那么您就不必花那么多时间来偏离您的实际任务。