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 .
Related
I have a scroll view and inside the scrollview I am having a content view with few subview inside this content view. My requirement is to zoom the content view but not the subview of content view.
Can anyone faced this before, or did the same. Any help would be appreciated.
Thanks in advance.
Scroll view just apply transform to contentView. This transform applied to all children in contentView. So you can apply inverted transform to children to negate parent transform.
func scrollViewDidZoom(_ scrollView: UIScrollView) {
guard let content = viewForZooming(in: scrollView) else {
return
}
let t = content.transform.inverted()
for v in content.subviews {
v.transform = t
}
}
I have a view and within an image that works as a button. I would like to know if there is a way to lock the size of the button so that when I zoom in, the view remains small and does not enlarge with the view..
I thought it was hard to manager in the beginning. But finally, if you put the imageView in a UIScrollView, It's not hard to achieve.
The idea is move the buttonView outside of imageView during zooming and when zoom is over, put it back to imageView to pretend nothing happened. I know it's too verbose in programming but actually it works perfectly for your case.
var originalCenter : CGPoint! // The center of ButtonView in imageView.
//All functions are from the UIScrollViewDelegate.
func viewForZooming(in scrollView: UIScrollView) -> UIView?{
originalCenter = buttonView.center // remember the original position.
return imageView
}
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
buttonView.frame = imageView.convert(buttonView.frame, to: scrollView)
scrollView.addSubview(buttonView)//add to superView of imageImage.
}
func scrollViewDidZoom(_ scrollView: UIScrollView){
buttonView.center = imageView.convert(originalCenter, to: scrollView) //During Zooming, update the buttonView in ScrollView.
}
func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat){
buttonView.frame = imageView.convert(buttonView.frame, from: scrollView)
imageView.addSubview(buttonView) //put it back.
}
I know it's better to use a parameter to control such operation. But I have-not found one according to public APIs. Maybe there is a better way, hope this one is your answer too.
Because you called transform for superView. It will make all subViews inside transform together.
You need to remake you views:
SuperView:
- Content view (the image view)
- Border view
- Button close
When you want to zoom the image, you only need to reset the superview frame.
You found a git SPUserResizableView.
I am using a viewController to handle two ChildViewControllers, each containing a UITableView. Would it be possible to set the the position y of a SubView of viewController (e.g. a UILabel) depending on the scrollView.contentOffset of the current ChildViewController?
It works fine with its own subviews already,..
func scrollViewDidScroll(_ scrollView: UIScrollView) {
self.testConstt.constant = scrollView.contentOffset.y
}
Thanks for helping!
Just observe the correct scroll view using a conditional statement. I assume the scroll views of the children are table views, so you may do something like this:
let tableViewA = UITableView(...)
let tableViewB = UITableView(...)
let someScrollView = UIScrollView()
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == tableViewA {
// observe a specific table view's scroll view and do something
} else if scrollView == someScrollView {
// observe a specific scroll view and do something
}
}
Remember, UITableView is a subclass of UIScrollView so they can be treated the same in scrollViewDidScroll(_ scrollView:).
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.
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
}
}