I have a UICollectionView with 3 full-screen cells and want to know when a cell is focused full-screen upon swipe. The cellForItemAtIndexPath.isFocused method isn't working for me. Is this the correct use of isFocused method or should I do it another way?
What ever, I can get from your question. I think you want to know on which page it is. So, Here is how i did it:
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
for cell in colViewSol.visibleCells {
let indexPath = colViewSol.indexPath(for: cell)
// here add the code whatever you want to do
}
}
Related
Below view is a tableViewController which is implemented and set the delegate and datasource methods and it works perfectly fine but I have no idea how to implement the purple view behavior on scrolling up and down.
How can I implement it?
The below code is the way I detect that view scrolled up or down. but still, no clue to implement this behavior.
override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
let translation = scrollView.panGestureRecognizer.translation(in: scrollView.superview)
if translation.y > 0 {
presenter?.viewScrolledDown()
} else {
presenter?.viewScrolledUp()
}
}
this question covers how to animate a backgroundColor. If the purple view is a UIView, this won't be a problem. It starts behind the tableView and then moves to the front of the tableView. You can handle this by using these functions:
view.bringSubviewToFront(purpleView)
view.sendSubviewToBack(tableView)
You will also need to animate the autoLayout constraints to move the bottom of the purple view up to the top of the tableView.
I have a scrollview in a ViewController. The scrollview has a UIScrollViewDelegate.On upon that i am adding a Tableview like below:
self.view.addSubview(self.tableView)
But when i am scrolling on tableview cells somehow the scrollview delegate is getting event, which is unexpected.
What can i do to prevent that?
The UITableView is the subclass of UIScrollView and that is the reason you are getting calls to the UIScrollViewDelegate when you are scrolling the cells.
You can not prevent this if you want to have the scrolling enabled for the TableView. I would suggest you check this condition inside your delegate method, for example:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard scrollView != self.tableView else {return}
}
That's normal because UITableView is a subclass of UIScrollView. Thus you should check if scrollView != self.tableView then do what you want to do inside the scrollView delegate method.
In my app, I have a screen like below:
I want to implement a stretchy header for my tableView which must stretch the imageViews embedded in a collectionView which is embedded in the tableViewHeader.
I tried to use the UIScrollViewDelegate in the tableView but it's totally buggy because I should modify the collectionView + cell at the same time:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let scrollY = scrollView.contentOffset.y
if scrollY < 0 {
var tmpFrame = tableViewHeader.frame
tmpFrame.origin.y = scrollY
tmpFrame.size.height = tableViewHeader.frame.height - scrollY
tableViewHeader.frame = tmpFrame
}
}
Do you know how to implement this feature or have you some hint to help me. Thank you.
If you don't have to use UICollectionView, but you can acomplish this with UIPageView or UIScrollView.
You can use UIPageViewController and embed it as subview to 'UITableView', so it will not only be stretchy, but will save its vertical pin gesture along with horizontal on 'UIPageViewController'.
P.S: I've been struggling with the same problem and implemented sample project.
I have an UIViewController containing a MKMapView and an UITableView. Each cell in the tableView contains a set of coordinates. If you want to see, please check this screenshot
I want to the update MKMapView "in real time" as the tableview is scrolling with contains coordinates.
I don't need to use the didSelectRow method. Because user does not select yet.
What is the right code blocks for scrollViewWillBeginDragging method?
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
print("scrollViewWillBeginDragging")
}
Thank you
I propose the following as one possibility.
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
print("scrollViewWillBeginDragging")
for visibleIndexPath in self.tableView.indexPathsForVisibleRows! {
print(visibleIndexPath)
// 1. Get coordinates from indexPath
// e.g. restaurants[indexPath.row]
// 2. Add annotation to a map view
}
}
I want to animate a UITableViewCell whenever it is about to leave the view. I know that there is a didEndDisplayingcell function, but by then, it is too late to animate the cell. Becuase it's gone. I basically want to cell to scale down using...
UIView.animate(withDuration: 0.2) {
self.cellView.transform = CGAffineTransform(translationX: self.view.frame.size.width, y: 0)
}
...and whenever it comes back on the screen, scale it back to normal.
Any ideas?
Not a complete solution, but this might get you there:
self.tableView.indexPathsForVisibleRows
Use this to find whats on the screen.
for path in self.tableView.indexPathsForVisibleRows! {
//check if it's past a point on the screen
//animate if needed
}
Check for these conditions in
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
// call the function to check if cells pass a threshold
}
The last bit will be setting it up so cells are fully visible on first load, but after that they start off in the state to be animated in.
The best way to approach this would be to check when the cell is finished being displayed via the UITableViewDelegate method:
func tableView(UITableView, didEndDisplaying: UITableViewCell, forRowAt: IndexPath)
Tells the delegate that the specified cell was removed from the table.