anish parajuli anish parajuli - 6 months ago 212
iOS Question

Animate headerView of TableView while pulling down UITableView in swift

I am trying to create a growing

UITableViewHeader
on
UITableView
. I have a UITableView and a mapView set in the tableHeaderView of UITableView.

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


And also implemented
scrollViewDidScroll
delegate and whenever it scrolls down, I have changed the frame of headerview as

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
}


However, it does not grow as expected without bouncing.Seems very unclear. Any help?

I am following these tutorials to create a Growing UITableViewheader when pulling down as
UITableVIew header without bouncing when pull down ,
Expand UITableView Header View to Bounce Area When Pulling Down

Here is the link of the project :
https://drive.google.com/file/d/0B6dTvD1JbkgBVENUS1ROMzI0Wnc/vie

EDITED: i somehow managed to have the effect but the animation seems very slow

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

Answer

Actually the animation was not working well in simulator of xcode6.3. I tried 2 days for this and posted a bounty here but when i finally I tested it on real device and found the MapView was properly bouncing.If anyone needs it..here is the piece of logic.

 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.
        }

    }
Comments