【问题标题】:Animate headerView of TableView while pulling down UITableView in swift快速拉下 UITableView 时为 TableView 的 headerView 设置动画
【发布时间】:2015-05-16 10:35:57
【问题描述】:

我正在尝试在UITableView 上创建一个不断增长的UITableViewHeader。我在 UITableView 的 tableHeaderView 中设置了一个 UITableView 和一个 mapView。

tblView.bounces = true
tblView.bouncesZoom = true
tblView.alwaysBounceVertical = true
mapView.frame = CGRectMake(0, 0, self.view.frame.size.width, CGFloat(kMapHeaderHeight))
mapView.mapType = MKMapType.Standard
mapView.zoomEnabled=true
mapView.scrollEnabled = true
mapView.delegate = mapHelper
tblView.tableHeaderView = mapView

并且还实现了scrollViewDidScroll委托,每当它向下滚动时,我都会将headerview的框架更改为

func scrollViewDidScroll(scrollView: UIScrollView) {
    var scrollOffset = scrollView.contentOffset.y
    println("\(scrollOffset)")
    var headerFrame : CGRect = self.mapView.frame
    if (scrollOffset < 0){
        headerFrame.size.height  -= scrollView.contentOffset.y/3
    }
    self.mapView.frame = headerFrame
}

但是,如果没有弹跳,它不会像预期的那样增长。看起来很不清楚。有什么帮助吗?

当下拉为 UITableVIew header without bouncing when pull down , Expand UITableView Header View to Bounce Area When Pulling Down

这是项目的链接: https://drive.google.com/file/d/0B6dTvD1JbkgBVENUS1ROMzI0Wnc/vie

已编辑:我以某种方式设法产生了效果,但动画似乎很慢

func scrollViewDidScroll(scrollView: UIScrollView) {
        let yPos: CGFloat = -scrollView.contentOffset.y

        if (yPos > 0) {
            var mapViewRect: CGRect = self.mapView.frame
            mapViewRect.origin.y = scrollView.contentOffset.y
            mapViewRect.size.height = kHeaderHeight+yPos
            self.mapView.frame = mapViewRect
        }
    }
let kHeaderHeight:CGFloat = 200

【问题讨论】:

标签: ios uitableview swift uiscrollview mkmapview


【解决方案1】:

我建议你使用this教程。

其中最重要的部分:

  • 创建一个scrollView(或任何UIScrollView 的子类)和一个单独的视图(将用作headerView,所以我们调用headerView)
  • 将 headerView 和 scrollView 添加为视图的子视图
  • 实现scrollViewDidScroll方法,并将框架逻辑放在那里(当然,如果你使用autolayout,你必须在那里管理约束)

【讨论】:

  • 你知道我试过这个..但没有工作..你能帮我更多吗?
  • @anishparajuli 你的问题到底是什么?
  • @anishparajuli 您是否为 mapView 设置了约束?或者你是如何设置 mapView 的?
  • 我已经以编程方式完成了。问题中有项目的链接
  • 我正在通过手机查看此内容,因此无法解压缩这些文件。
【解决方案2】:

实际上动画在 xcode6.3 的模拟器中效果不佳。我为此尝试了 2 天并在此处发布了赏金,但是当我最终在真实设备上对其进行测试时,发现 MapView 可以正常弹跳。如果有人需要它..这是逻辑。

 let kHeaderHeight:CGFloat = 380
    class NewBookingVC: UIViewController {

        @IBOutlet weak var tblView: UITableView!
        let mapView : MKMapView = MKMapView()

        var customTableHeaderView:UIView = UIView()
        override func viewDidLoad() {
            super.viewDidLoad()

            tblView.delegate = self
            tblView.dataSource = self
            mapView.frame = CGRectMake(0, 0, self.view.frame.size.width, 380)
            mapView.mapType = MKMapType.Standard
            mapView.zoomEnabled=true
            mapView.scrollEnabled = true

            customTableHeaderView = UIView(frame: CGRectMake(0, 0, self.view.frame.size.width, 380))
            customTableHeaderView.addSubview(mapView)
            tblView.tableHeaderView = customTableHeaderView

        }

        func scrollViewDidScroll(scrollView: UIScrollView) {
            let yPos: CGFloat = -scrollView.contentOffset.y

            if (yPos > 0) {
                var mapViewRect: CGRect = self.mapView.frame
                mapViewRect.origin.y = scrollView.contentOffset.y
                mapViewRect.size.height = kHeaderHeight+yPos
                self.mapView.frame = mapViewRect
            }
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }

    }

【讨论】:

    【解决方案3】:

    其实你要做的就是为table view实现scrollview delegate,因为它继承自scrollview,你可以实现scrollViewDidScrolldelegate,每当它向下滚动时,改变headerview的框架。

    这背后的想法是在滚动时您始终必须将MKMapView 的 y 位置保持在零位置......并且高度应该相应增加......

    func scrollViewDidScroll(scrollView: UIScrollView) {
                let yPos: CGFloat = -scrollView.contentOffset.y
    
                if (yPos > 0) {
                    //adjust your mapview y-pos and height
                    //adjusting new position is all about real math
                 }
            }
    

    iOS 模拟器中的性能预计不会与设备上的性能相匹配。 iOS 模拟器旨在作为快速原型设计和快速迭代的工具。在您的情况下,在模拟器中重绘 MapView 性能也很慢,因为在每次滚动时,您都在计算它的新框架和高度。所以调整新框架需要一些时间,而且看起来很慢。

    但是,在真实设备上进行调优时效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多