Ios 在集合视图中预加载MKMapView
我在尝试做“标签”体验。每个选项卡都有屏幕宽度,用户可以在每个选项卡之间滑动。一切正常,除了索引为“3”的选项卡上实现了MKMapView 当用户打开应用程序时,集合视图被初始化为索引“1”,并且Ios 在集合视图中预加载MKMapView,ios,swift,uicollectionview,mkmapview,uicollectionviewcell,Ios,Swift,Uicollectionview,Mkmapview,Uicollectionviewcell,我在尝试做“标签”体验。每个选项卡都有屏幕宽度,用户可以在每个选项卡之间滑动。一切正常,除了索引为“3”的选项卡上实现了MKMapView 当用户打开应用程序时,集合视图被初始化为索引“1”,并且索引XPath(行:1,节:0) 问题是,当用户从1->2刷卡时,UI会被阻塞大约一秒钟,然后集合视图会触发CellForIndexAtdeXPath调用IndexXPath(行:2,节:0)。我在那个索引处尝试“强制”称重传感器,但不幸的是,地图似乎在屏幕上渲染之前就被初始化了。有什么解决办法吗 编
索引XPath(行:1,节:0)
问题是,当用户从1->2刷卡时,UI会被阻塞大约一秒钟,然后集合视图会触发CellForIndexAtdeXPath
调用IndexXPath(行:2,节:0)
。我在那个索引处尝试“强制”称重传感器,但不幸的是,地图似乎在屏幕上渲染之前就被初始化了。有什么解决办法吗
编辑:
斯威夫特
class TabCell : UICollectionViewCell
{
var nearestView : NearestView?
var stationsView : StationsView?
var stationsMapView : MapView?
override init(frame: CGRect)
{
super.init(frame: frame)
let gradient = CAGradientLayer()
gradient.frame = bounds
gradient.colors = [UIColor.clear, RuntimeResources.Get(color: .VeryDarkBlue).cgColor]
layer.insertSublayer(gradient, at: 0)
}
required init?(coder aDecoder: NSCoder)
{
fatalError("init(coder:) has not been implemented")
}
func SetupNearestView()
{
if (nearestView != nil)
{
return
}
nearestView = NearestView(frame: CGRect(x: 0, y: 0, width: frame.width, height: frame.height))
addSubview(nearestView!)
addConstraintsWithFormat(format: "H:|[v0]|", views: nearestView!)
addConstraintsWithFormat(format: "V:|[v0]|", views: nearestView!)
}
func SetupMapView()
{
if (stationsMapView != nil)
{
return
}
stationsMapView = MapView(frame: CGRect(x: 0, y: 0, width: frame.width, height: frame.height))
addSubview(stationsMapView!)
addConstraintsWithFormat(format: "H:|[v0]|", views: stationsMapView!)
addConstraintsWithFormat(format: "V:|[v0]|", views: stationsMapView!)
}
func SetupStationsView()
{
if (stationsView != nil)
{
return
}
stationsView = StationsView(frame: CGRect(x: 0, y: 0, width: frame.width, height: frame.height))
addSubview(stationsView!)
addConstraintsWithFormat(format: "H:|[v0]|", views: stationsView!)
addConstraintsWithFormat(format: "V:|[v0]|", views: stationsView!)
}
}
斯威夫特地图
import UIKit
import MapKit
class MapView: MKMapView, MKMapViewDelegate
{
private let infoViewHeight = CGFloat(500)
private let FocusZoomLevel = 0.01
private let ZoomLevel = 0.04
private let centerOfSzczecin = CLLocationCoordinate2D(latitude: 53.433345, longitude: 14.544139)
private var infoViewTopConstraint : NSLayoutConstraint?
lazy var mainStationView : UIView = {
let msv = UIView(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: self.infoViewHeight))
msv.translatesAutoresizingMaskIntoConstraints = false
msv.backgroundColor = RuntimeResources.Get(color: .DarkBlue)
return msv
}()
override init(frame: CGRect)
{
super.init(frame: frame)
delegate = self
// Adding info view
SetupInfoView()
// Setting center of map
let span = MKCoordinateSpanMake(ZoomLevel, ZoomLevel)
let region = MKCoordinateRegion(center: centerOfSzczecin, span: span)
setRegion(region, animated: false)
}
required init?(coder aDecoder: NSCoder)
{
fatalError("init(coder:) has not been implemented")
}
private func SetupInfoView()
{
addSubview(mainStationView)
infoViewTopConstraint = mainStationView.topAnchor.constraint(equalTo: self.bottomAnchor)
infoViewTopConstraint?.isActive = true
mainStationView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
mainStationView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
mainStationView.heightAnchor.constraint(equalToConstant: infoViewHeight).isActive = true
}
}
因为您没有重用MapView单元 尝试将地图视图作为
UICollectionViewCell
(在UIViewController
中)外部的变量加载,并将其作为子视图添加到中的单元格中
在这里,你可以把它从细胞中移除
只需将所有mapView代码加载到单元格外,然后使用单元格将地图显示为子视图即可。如果您的UI被阻止,则说明您在此处出错:cell.SetupMapView()与预加载无关。对此我不确定。我正在创建新的MapView实例并处理其约束,这里没有额外的工作。无需加载数据、无需绘制注释、无需cpu密集型或阻塞任务。首先,您可以更轻松地添加问题所需的代码。其次,每次调用cell.SetupMapView()函数cell ForRowaItem时,是否进行检查以避免每次调用cell时重复所有操作?我将在短时间内发布更多代码。确保我做了足够的检查,我的问题在每次应用程序运行时只发生一次。只是添加了一些源代码。你说得对!初始化map的对象确实是CPU密集型的,我在父ViewController的内部创建了这个初始值设定项,并且只将其作为子视图添加到单元格中,现在工作起来非常有魅力。谢谢@马辛很乐意帮忙:)
import UIKit
import MapKit
class MapView: MKMapView, MKMapViewDelegate
{
private let infoViewHeight = CGFloat(500)
private let FocusZoomLevel = 0.01
private let ZoomLevel = 0.04
private let centerOfSzczecin = CLLocationCoordinate2D(latitude: 53.433345, longitude: 14.544139)
private var infoViewTopConstraint : NSLayoutConstraint?
lazy var mainStationView : UIView = {
let msv = UIView(frame: CGRect(x: 0, y: 0, width: self.frame.width, height: self.infoViewHeight))
msv.translatesAutoresizingMaskIntoConstraints = false
msv.backgroundColor = RuntimeResources.Get(color: .DarkBlue)
return msv
}()
override init(frame: CGRect)
{
super.init(frame: frame)
delegate = self
// Adding info view
SetupInfoView()
// Setting center of map
let span = MKCoordinateSpanMake(ZoomLevel, ZoomLevel)
let region = MKCoordinateRegion(center: centerOfSzczecin, span: span)
setRegion(region, animated: false)
}
required init?(coder aDecoder: NSCoder)
{
fatalError("init(coder:) has not been implemented")
}
private func SetupInfoView()
{
addSubview(mainStationView)
infoViewTopConstraint = mainStationView.topAnchor.constraint(equalTo: self.bottomAnchor)
infoViewTopConstraint?.isActive = true
mainStationView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
mainStationView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
mainStationView.heightAnchor.constraint(equalToConstant: infoViewHeight).isActive = true
}
}