Move a View When Scrolling in Uitableview

Move a view when scrolling in UITableView

Since UITableView is a subclass of UIScrollView, your table view's delegate can receive UIScrollViewDelegate methods.

In your table view's delegate:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
static CGFloat previousOffset;
CGRect rect = self.view.frame;
rect.origin.y += previousOffset - scrollView.contentOffset.y;
previousOffset = scrollView.contentOffset.y;
self.view.frame = rect;
}

How to overwrite another view when UITableView scrolls

  1. Move blue view on top of tableView
    Sample Image

  2. Layout your blue view's bottom equal to tableView's bottom

  3. tableView's background color is clear

Scrolling collection view with UIViews on top?

I have achieved the same requirement through some basic steps as below.

//Declare the view which is going to be added as header view
let requiredView = UIView()

//Add your required view as subview of uicollectionview backgroundView view like as
collectionView.backgroundView = UIView()
collectionView.backgroundView?.addSubview(requiredView)

//After that control the frame of requiredHeaderView in scrollViewDidScroll delegate method like
func scrollViewDidScroll(scrollView: UIScrollView) {
let per:CGFloat = 60 //percentage of required view to move on while moving collection view
let deductValue = CGFloat(per / 100 * requiredView.frame.size.height)
let offset = (-(per/100)) * (scrollView.contentOffset.y)
let value = offset - deductValue
let rect = requiredView.frame
self.requiredView.frame = CGRectMake(rect.origin.x, value, rect.size.width, rect.size.height)
}

Animate UIView position on tableview scroll and again tableview drag

It's hard to say exactly what you are looking for but this might work for you. In this implementation when the user drags up the header will hide and when the user drags down the header will show. This is true regardless of the scroll position.

I made a change to one of your constraints so that the top of the tableView is pinned to the bottom of the header:

    tableViewTopConstraint = tableView.topAnchor.constraint(equalTo: headerViewOne.bottomAnchor)

I'm also tracking the state of the header like so:

enum HeaderState {
case hidden
case revealed
case hiding
case revealing
}

var headerState: HeaderState = .revealed

Now for the scrolling logic. If the scroll position is near the top then we want to manually show/hide the header. However, if the scroll position is near the bottom or center then we want to animate the show/hide functionality.

func scrollViewDidScroll(_ scrollView: UIScrollView) {
let translation = scrollView.panGestureRecognizer.translation(in: scrollView.superview)

if scrollView.contentOffset.y < 184 {
if translation.y > 0 && headerState != .revealed && headerState != .revealing {
headerViewOneTopConstraint.constant = max(-184, min(0, -scrollView.contentOffset.y))
} else if translation.y < 0 {
headerViewOneTopConstraint.constant = max(-184, min(0, -scrollView.contentOffset.y))
}
return
}

if translation.y > 0 && headerState == .hidden {

self.headerState = .revealing
UIView.animate(withDuration: 0.4, animations: {
self.headerViewOneTopConstraint.constant = 0
self.view.layoutIfNeeded()
}, completion: { _ in
self.headerState = .revealed
})

} else if translation.y < 0 && headerState == .revealed {

self.headerState = .hiding
UIView.animate(withDuration: 0.4, animations: {
self.headerViewOneTopConstraint.constant = -184
self.view.layoutIfNeeded()
}, completion: { _ in
self.headerState = .hidden
})
}
}

Give this a shot and see if it meets your needs.



Related Topics



Leave a reply



Submit