【问题标题】:Xcode, remove subViewXcode,删除子视图
【发布时间】:2018-02-07 02:26:21
【问题描述】:

我有一个应用可以显示不同时间的家庭位置。我使用 Timer 每 1 秒提前一次日期,并且每年都会从 Core Data 中检索数据,并在视图上绘制一个带有 subView 的对象,覆盖地图。我无法理解的是如何在每年前进时清除子视图。欢迎提出建议。

定时器代码:

  @IBAction func startCount(_ sender: Any) {

        if !counting {
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(counter), userInfo: nil, repeats: true)
        counting = true
    }
}

计数器代码:

  @objc func counter() -> Void {
    yearCount.text = String(countYear)
    compareYear = String(countYear)
    print(compareYear)
    drawCircles()
    countYear += 1
}

画圈代码:

  func drawCircles() {

         if markerView != nil {
        if let markerView = view.viewWithTag(countYear-1){
        markerView.removeFromSuperview()
        }
    }

    var markers = [PlaceNames]()
    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "PlaceNames")
    let predicate = NSPredicate(format: "year == %@", compareYear)
    fetchRequest.predicate = predicate

    do {
        markers = try coreDataStack.context.fetch(fetchRequest) as! [PlaceNames]
        print(markers.count)

    } catch let error as NSError {
        print("Could not fetch \(error), \(error.userInfo)")
    }

        for marker in markers {

            placeEastings = marker.value(forKey: "eastings") as? Float
            placeNorthings = marker.value(forKey: "northings") as? Float
            print(placeEastings, placeNorthings)

            if (placeEastings != nil && placeNorthings != nil) {

            let tempXAxis = placeEastings
            placeXAxis = (tempXAxis! - 6.0)/1.5
            let tempYAxis = placeNorthings
            placeYAxis = (tempYAxis! - 6.0)/1.5
                print(placeXAxis, placeYAxis)
            }

            markerView = UIImageView(frame: CGRect(x: Double(placeXAxis), y: Double(placeYAxis), width: 12.0, height: 12.0))

            let renderer = UIGraphicsImageRenderer(size: CGSize(width: 12, height: 12))

            markerImage = renderer.image { ctx in
                let rectangle = CGRect(x: 0, y: 0, width: 12, height: 12)
                ctx.cgContext.setFillColor(UIColor.white.cgColor)
                ctx.cgContext.setStrokeColor(UIColor.black.cgColor)
                ctx.cgContext.setLineWidth(1)

                ctx.cgContext.addEllipse(in: rectangle)
                ctx.cgContext.drawPath(using: .fillStroke)
            }

            markerView.tag = countYear
            markerView.image = markerImage

            self.view.addSubview(markerView)
    }
}     

【问题讨论】:

    标签: swift xcode addsubview subviews


    【解决方案1】:

    我认为您可以将所有标记视图存储在数组中,例如var markerViews: [UIImageView],然后

    @objc func counter() -> Void {
        yearCount.text = String(countYear)
        compareYear = String(countYear)
        print(compareYear)
    
        clearExistingMarkerViews()
        drawCircles()
        countYear += 1
    }
    
    private func clearExistingMarkerViews() {
        markerViews.forEach { $0.removeFromSuperView() }
       markerViews.removeAll()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 2011-12-06
      • 2012-04-06
      • 2023-03-21
      • 2011-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多