0

"탭"경험을하려고합니다. 각 탭은 화면 너비를 가지며 사용자는 각 탭 사이를 스 와이프 할 수 있습니다. MKMapView가 구현 된 인덱스 "3"이있는 탭을 제외하면 모든 것이 올바르게 작동합니다.컬렉션보기에서 MKMapView 미리로드

enter image description here

사용자 오픈 앱, 컬렉션 뷰 IndexPath(row: 1, section: 0)

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
{ 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PageIndentifier, for: indexPath) as! TabCell 

    if (indexPath.item == 0) 
    { 
     cell.SetupNearestView() 
     NearestView = cell.nearestView 
     NearestView?.Delegate = self 
    } 

    if (indexPath.item == 1) 
    { 
     cell.SetupStationsView() 
     StationsView = cell.stationsView 
     StationsView?.Delegate = self 
    } 

    if (indexPath.item == 2) 
    { 
     cell.SetupMapView() 
     MapView = cell.stationsMapView 
    } 

    return cell 
} 

문제점에 대한 인덱스 "1"로, 또한 cellForIndexAtIndexPath이 호출을 초기화 할 때 UI가 약 제 경우 사용자 와이프 차단된다는 것이다 1 -> 2이면 컬렉션보기 트리거 cellForIndexAtIndexPathIndexPath(row: 2, section: 0)에 대해 호출됩니다. 그 인덱스에서 "강제"로드 셀을 시도했지만 unfortunatelly지도는 화면에 렌더링되기 전에 초기화 된 것처럼 보입니다. 거기에 대한 해결 방법이 있습니까?

편집 : 당신이지도보기 셀을 재사용하지 않기 때문에

TabCell.swift

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!) 
} 
} 

MapView.swift

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 
} 

} 
+0

UI가 차단 된 경우 cell.SetupMapView()는 미리로드하는 것과 아무런 관련이 없습니다. –

+0

나는 이것에 대해 잘 모르겠습니다. 나는 새로운 MapView 인스턴스를 생성하고 이에 대한 제약 조건을 조작하고 있습니다. 추가 작업은 없습니다. 데이터 로딩, 주석 그리기, CPU 집중 또는 차단 작업이 없습니다. – Marcin

+0

먼저 질문을 호출하는 코드를 쉽게 추가 할 수 있습니다. 둘째로 cellForRowAtItem이 호출 될 때마다 cell.SetupMapView() 함수를 호출합니다. 셀을 호출 할 때마다 모든 것을 다시 실행하지 않도록 검사합니까? –

답변

0

.

는 (당신의 UIViewController에서) UICollectionViewCell의 외부 변수 로지도보기를로드 시도하고 collectionView:willDisplayCell:forItemAtIndexPath:

의 셀에 하위 뷰로 추가하고 collectionView:didEndDisplayingCell:forItemAtIndexPath:에 당신은 셀에서 제거 할 수 있습니다.

모든 mapView 코드를 셀 외부에로드하고 Cell을 사용하여 맵을 하위보기로 표시하면됩니다.

+1

당신이 옳았 어! 맵의 객체 초기화는 실제로 CPU를 많이 사용합니다. 부모 VIEWController의 내부 초기화 프로그램을 만들고 하위 뷰로만 셀에 추가했습니다. 이제는 매력처럼 작동합니다. 감사! – Marcin

+0

@Marcin 도와 주셔서 감사합니다 :) –