Updating MKMapView while UITableView is scrolling, obtaining coordinates from cell - ios

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

Related

UIScrollView Delegate Re-Routing - To check the state of UIScrollView

I have a tableView and button on the same view (Siblings),
i need to HIDE the button when the tableView is SCROLLING (Up and Down Direction no problem) and SHOW when it is STOPPED SCROLLING
I tried the following code, but it is not working (Correctly)
func scrollViewDidScroll(_ scrollView: UIScrollView) {
self.vcView.hideButton()
}
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
self.vcView.showButton();
}
After a lot of search i found this article, but it is in Objective C, can anyone help me to convert it in to Swift 3.
Why not simply implement these two delegate methods:
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
self.vcView.hideButton()
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
self.vcView.showButton();
}
If you're only looking to show and hide a button or any view while scrolling, this will work. Just be sure to set the scrollview delegate and it should work without issues.
Theres no need to convert that objc code to swift.
https://screencast.com/t/eTq9Nzfsgs6E

How to scroll two UITableViews symmetrically?

In my app I have two table views. Both are same height and width and same cell count.
I want when tableview A Scroll Simultaneously tableview B also scroll.
You can use scrollview delegate scrollViewDidScroll for this purpose. This will do the exact thing you want.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == self.tableA{
self.tableB.setContentOffset(self.tableA.contentOffset, animated: false)
}
else if scrollView == self.tableB{
self.tableA.setContentOffset(self.tableA.contentOffset, animated: false)
}
print("scrollViewDidScroll") // to check whether this function is called while scrolling
}
TRY THIS:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
<your tableView Name>.contentOffset = scrollview.contentOffset
}
As Tableview inherits from ScrollView. Try this.
FOR SWIFT 4:
func scrollViewDidScroll(_ scrollView: UIScrollView)
{
<your tableView Name>.contentOffset = scrollview.contentOffset
}
Instead of using tableview you can use UIPickerView with two components.
If you need code let me know.

How to custom header view that sticky in top screen when scroll UICollectionView?

I have the header (header : UICollectionReusableView) in my UICollectionView. The header has 3 UIView (UIView1, UIView2, UIView3). When I set collectionLayout.sectionHeadersPinToVisibleBounds = true, the header will in top when I scroll the collection. And I want when the header stick in top of UICollectionView, I will hide UIView1 and UIView2. How can I do that?
UICollectionView is a subclass of UIScrollView. This means that is you assign a delegate to it then this delegate may listen to scrollViewDidScroll(_ scrollView: UIScrollView) method which will be called every time the offset changes in your collection view.
On this event you will need to get all of your header views which I assume you may get by calling visibleSupplementaryViews on your collection view and check its class type.
If the received view is indeed your header you will need to check its frame in comparison to your collection view and see if its position is on top. To do so you may convert frame to your coordinates:
func isHeader(headerView: UIView, onTopOfCollectionView collectionView: UICollectionView) -> Bool {
guard let collectionSuperView = collectionView.superview else {
return false // Collection view is not in view hierarchy. This will most likely never happen
}
let convertedFrame = headerView.convert(headerView.bounds, to: collectionSuperView) // Convert frame of the view to whatever is the superview of collection view
return convertedFrame.origin.y <= collectionView.frame.origin.y // Compare frame origins
}
So I guess the whole thing would be something like:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
collectionView.visibleSupplementaryViews(<#My identifier here#>).forEach { view in
if let header = view as? MyHeaderView {
header.hideSecondaryViews = isHeader(headerView: header, onTopOfCollectionView: collectionView)
}
}
}
I hope this gets you on the right track.

Swift: UICollectionView Paging Enabled

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

Zoom in on all subviews in uiscrollview

is it possible to zoom in on all objects in a scrollview? The scrollview is created dynamically and can contain multiple different types of objects (labels, buttons, imageviews)
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return //all subviews in scrollview
}
Just create a view, add all subviews to this view and add the newly created view as a single subview to the scrollview...
Then in the viewForZoomingInScrollView delegate method return the object at Index 0:
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return scrollView.subviews[0]
}
I did not understand your question very well, but you can not zoom in if scrollView contain more than one image , Because the images will appear wasteful if used full image .

Resources