How to scroll multiple tableview simultaniously - ios

2 Tableviews in a ViewController with different size of cell height. When i scroll on the view controller , need to scroll both tableview cell at same time also both table cells also need to hide at same time.
200px height of cell in top tableview, 50px height of bottom tableview.
when i swipe 100px , top table cell scrolls 100 px and bottom table cell should be scrolled 25px.
is this parallel scrolling any possible? any suggestion..
Thanks in advance

As UITableview is derived from UIScrollview you can get the scroll amounts in the delegate method
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
You can get content offset of the scrollview which is being scrolled and assign that to the other tableview.
In this method you should be able to access both tableviews.
The property you need to check is contentOffset.
firsttableview.contentOffset = scrollview.contentOffset

You can use this function of UIScrollView to scroll multiples UITables parallel
func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView == tableView1 {
self.tableView1.contentOffset = CGPointMake(0, scrollView.contentOffset.y)
self.tableView2.contentOffset = CGPointMake(0, scrollView.contentOffset.y)
}
else if scrollView == self.tableView2!
{
self.tableView1.contentOffset = CGPointMake(0, scrollView.contentOffset.y)
}
else if scrollView == self.bottomMenu_grid!
{
print(scrollView.contentOffset.x)
self.tableView2.contentOffset = CGPointMake(scrollView.contentOffset.x, scrollView.contentOffset.y)
}
}

Related

how to check if UIScrollView content doesn't need to use scroll

My problem is I need to change the label text color below my scroll view if the content of my scroll view is not all visible.
For example: I have a label colored red, if all the content of the scroll view can be seen on the screen(not using scroll at all), and the label colored blue if all the content inside of my scroll view is not visible. how to do it programmatically?
NOTE: I have a contentView -> scrollView (programmatically) -> stackView (programmatically).
(stackview inside scrollview and scrollview inside contentview. and a label below the content view)
Thanks.
1: Get the height of the content view of scrollView
let totalHeight = scrollView.contentSize.height
2: In viewDidLoad check:
if totalHeight > scrollView.frame.size.height {
// can scroll more
} else {
// full content visible
}
3: once user starts scrolling, you can call:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if (scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)) {
//reached to bottom
} else {
// can scroll more
}
}
This solution worked for me, with the difference that I placed it in the viewDidAppear
if scrollView.contentSize.height > scrollView.visibleSize.height {
// can scroll more
} else {
// full content visible
}

Implement twitter Profile like view with parallax scroll

I want to implement the following sort of view where the view can be completely scrolled and houses 2 different scrollview (Main and the secondary) with infinite scrollable content. This represents the exact thing I want.
The red view is superview - should scroll vertically
The green view is of the height of the current view and is just static. That doesnt scroll
The blue view is the horizontal scrollview where for each label there is a yellow vertically scrolling infinity collection view
the labels scroll as in the given video. under each label there is the collection view I mentioned in point 3
The blue box is the scroll view and I want the scrolling to happen horizontally in a parallax way such as this.
I am able to implement the above parallax in the correct fashion but each title contains their own collectionview. When I implement this I am not able to have an infinite scroll. Below is the code for that :
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == containerScrollView {
for i in 0..<shotsData.count {
let label = scrollView.viewWithTag(i + tagValueL) as! UILabel
let view = scrollView.viewWithTag(i + tagValueV) as! ShotsMediaView
let scrollContentOffset = scrollView.contentOffset.x + scrollView.frame.width
let viewOffset = (view.center.x - scrollView.bounds.width/4) - scrollContentOffset
label.center.x = scrollContentOffset - ((scrollView.bounds.width/4 - viewOffset)/2)
}
}
}
How can I exactly achieve the same behavior with an infinite scroll vertically? I want each of these titles to have collectionview that have the dynamic height each.
I did a crude implementation of this.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == colorsCollectionView {
let newContentOffSetX = scrollView.contentOffset.x
let distance = contentOffSetX + newContentOffSetX
// Scroll the text collection view proportinately
let titleScrollDistance = (distance/colorsCollectionView.frame.width * 75.0)
titlesCollectionView.contentOffset = CGPoint(x: titleScrollDistance, y: titlesCollectionView.contentOffset.y)
contentOffSetX = newContentOffSetX
}
}
contentOffSetX is a property of the class(ViewController) that I use to keep track of the scrolling distance of the bottom collection view. Initially that is set to 0. When the user scrolls the collection view at the bottom, the above delegate method is called. Then I use the contentOffSet to get the distance that was scrolled along the X-axis. I map that to the width of the title labels(hardcoded as 75.0) to calculate the distance that collection has to be scrolled. Hope this can be refined to serve your purpose, but I am sure that there are better methods out there :)

Ignore vertical scrolling inside UITableViewCell

One of the cells in a UITableView contains a scroll view. I want to be able to scroll the content in the cell horizontally, but NOT vertically.
How can I achieve this?
Additionally, the scroll view is a subview of UIWebView, so I cannot control its content size.
I have tried setting the content offset directly, but this prevents the entire table from being scrolled. I want the table to scroll vertically, but not the content in the cell.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
DispatchQueue.main.async {
scrollView.contentOffset = CGPoint(x: scrollView.contentOffset.x, y: 0)
}
}
You will have to ensure the content is smaller than the vertical bounds of the UIScrollView to prevent vertical scrolling, and in addition you'll need to set scrollView.alwaysBounceVertical = false.

How to change the content offset of a UICollectionView simultaneously?

I have a horizontal scrollable collectionView with two cells that take up the screen. Inside those cells is an embedded collection view that scrolls vertically. Is there a way for me to scroll the vertical CV inside the first cell, while using its ContentOffset to scroll the vertical CV in the second cell?
Here's how I'm trying to accomplish it:
let scrollView = notification.userInfo!["scrollView"] as! UICollectionView
if scrollView.contentOffset.y <= 250.0 {
childViewControllerForPosts.collectionViewForGroups?.collectionView.setContentOffset(CGPointMake(0, scrollView.contentOffset.y), animated: false)
self.topVerticalConstraint?.constant = -scrollView.contentOffset.y
}
else {
if self.topVerticalConstraint?.constant > -250 {
// Make sure it stays at -250
self.topVerticalConstraint?.constant = -250
}
}
The FeedCell would be the main collection view (which horizontally scrolls) that contains the vertical scrolling collection view. I'm trying to access the second one to change the vertical contentOffset when I scroll the one in first one.
Here's how I'm trying to save the variable that should contain the second main collection view:
collectionViewForGroups = collectionView(collectionView!, cellForItemAtIndexPath: NSIndexPath(forItem: 1, inSection: 0)) as? FeedCell
EDIT:
Note that when I scroll horizontally, the vertical scroll view didn't change. But I want it to change with the one I just scrolled in the gif. If I were to scroll the 2nd vertical collection view, the UIView above will reappear because the topLayConstraint changes due to the vertical contentOffset change.

conflict scrolling scrollview and tableview

I have a scrollview that contain some element (uiimage, webview ,...)
in buttom of scrollview add tableview (comments list). Problem: although tableview is part of scrollview, but scrollview scroll separate and tableview scrolling separate!
I want at the end of scrollview and start tableview scrollview scrolling tableview and tableview scroll disabled.
I used it code:
Swift:
override func intrinsicContentSize() -> CGSize {
self.layoutIfNeeded()
return CGSizeMake(UIViewNoIntrinsicMetric, contentSize.height)
}
Objective C:
-(CGSize)intrinsicContentSize{
[self layoutIfNeeded];
return CGSizeMake(UIViewNoIntrinsicMetric, contentSize.height)}
but don't work.
thanks for help
That happens because it is the behavior of having a table view inside a scroll view. That should be happening.
Solution: Destroy the scrollView, and implement a tableView with a header view, wish that header view it will be the view with uiimage, webview etc... and the tableView it will be your comments. This is the best way of implementing what you want, that is if i understood right what you actually want.
Adding a header to a table view example:
self.tableView.tableHeaderView = topView // where top view is the view wish contains your uimage, buttons etc...
Avoid bounce of ScrollView when we scroll the tableview. I have added the below line of code.It worked for me.
self.scrollView.delegate = self
scrollView.contentSize = CGSize(width: self.view.frame.width, height: 500)
scrollView.showsVerticalScrollIndicator = true
scrollView.tag = 1
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if scrollView.tag != 1 {
self.scrollView.bounces = false
}
}
Thanks

Resources