I have many controls in the UIScrollView including few buttons. For an example I have a button at position (300,200) from where I am popping UIPopOverController to do required tasks but when I scroll scrollview is scrolled, popover still pops up from the location (300,200) which is wrong! Is there anyway to get button position in visible scroll view?
This is how I get the exact position of button while knowing it's initial position in UIScrollView. I just need to subtract current scrollview offset from button's Y coordinate! For me X coordinate doesn't change - as no horizontal scrolling supported.
CGPoint point = CGPointMake(self.button.frame.origin.x,
self.button.frame.origin.y - self.scrollView.contentOffset.y)
Maybe it pops ut at the wrong position because it is not added at the UIScrollView, but at its superview. Try to check in the views hierarchy the position of the button. Another thing you can do to retrieve the button position is to go inside the view hierarchy and extract the arrays of subviews, and check the position of the button here.
Take a look at this link http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW17
Use UIView convertRect:toView: or convertRect:fromView: method.
Related
Trying to create a slide-up view that will click into one of two places as shown in the below Stock App images. The view slides into the closest spot should it be let go. It also can be tapped on the top to move it automatically. Any ideas on where to get started? I haven't been able to find much help regarding this. Thanks in advance.
View is down
View is up
Make a view with a constant height constraint.
Add a UIPanGestureRecognizer to the view.
As the user pans you adjust the height constraint by the gesture offset, clamping at the min and max height.
When the user lets go you animate the current height to the closest height (min or max).
have looked but can't find a solution to this on SO.
The problem: I would like to add buttons to a UIImageView that is zoomable however when the image zooms in, the buttons remain in the same place on the image but do not zoom (ie they stay the same size). This is similar to what Maps does if it has many pins on the map, when you zoom in on the Map if the pins are close together they spread apart and stay the same size, rather than zooming to huge sizes with the map!
My attempt: I understand how to make the image zoom, through use of a scroll view however I am unsure of what the view hierarchy should be. The button locations depend on the image and so you cannot add the buttons to just the scroll as they wouldn't move when zooming. However, if I add the buttons to the image view then they zoom with the image (since I return the image view for the delegate method viewForZoomingInScrollView). I have tried tinkering with an overlayer for the buttons with clues from Zooming a background image without zooming the overlay but haven't found anything that works yet.
Any help is greatly appreciated, thanks a lot!
You should not add the button as a subview to the scroll view. Instead, try adding the button as a subview to your scroll view's superview.
[superview addSubview:scrollView];
[superview addSubview:button];
Your view hierarchy will look like this:
<superview>
<scrollView>
<button>
Since the button is not a subview of the scroll view, it won't get zoomed.
I'm trying to create a specific layout in an iOS App, where the "background" is a MKMapView and the overlay is a UIScrollView. The idea is that there are two screens, one with a map, and another with some additional information on. The user can scroll down to see this general information, and this view covers up the map and has a blurred background to create some depth.
Here's an image showing the layout:
As you can see, the map is fixed in its position. It will always be behind the content, even if you scroll. I want the map to respond to gestures, but only when used directly on the map. When the user "scrolls" over the bottom bar, I want the content to scroll up, revealing the second page which then covers the map.
I'm lost at what to do to achieve this. I tried putting the map on the original UIView and then covering it up with a scrollview, but this causes the map not to respond to gestures. I only want the UIScrollView to respond to gestures when it's on either the bottom bar, or the second page (each is its own separate view). Otherwise I want the Map to respond to the gestures.
I hope I managed to explain it well, if not, please do not hesitate to ask questions. I appreciate all help!
It sounds like you are adding a full screen UIScrollView on top of a MKMapView and the scrollview is picking up all the touches.
What you are asking about is not necessarily standard so there are multiple ways to implement it and you have to decide what works best for your use case.
Here is what I would do:
Use constraints on the scrollview to ensure that it is only covering the bottom bar area.
Make sure you can toggle the constant of the constraint controlling the scroll view's height (hook up an IBOutlet if you're using storyboard).
Add both a swipe gesture recognizer and a tap gesture recognizer that will fire toggleScrollViewFullScreen if recognized. Ensure that these recognizers can only be recognized while the scrollView is the bottom bar.
Have an X-out button display in the top corner while the scrollview is in full screen. This button can call toggleScrollViewFullScreen to dismiss the view back down.
Example toggleScrollViewFullScreen method:
//Toggle size of scroll view
- (void) toggleScrollViewFullScreen {
CGFloat bottomBarHeight = 100;
if (self.scrollViewHeightConstraint.constant > bottomBarHeight){
self.scrollViewHeightConstraint.constant = bottomBarHeight
}else {
self.scrollViewHeightConstraint.constant = self.view.bounds.size.height;
}
//Animate constraint change:
[UIView animateWithDuration:1 animations:^{
[self.view setNeedsUpdateConstraints];
[self.view setNeedsLayout];
}];
}
Its been a pain in the ass to get a working solution for this problem. On a view i have around five scroll views with maximum zoom upto 5 . What i want is if a user has zoomed into a particular region of image , and then comes back , scroll view sets the image wherever it was left . I have tried using content Offset and content Inset property but that does'nt help.
All you need to do is to save somewhere both the value of the scroll view's contentOffset property and zoomScale.
When you want to move your scrollview back to that position you just do
[myScrollView setZoomScale:lastZoomScale];
[myScrollView setContentOffset:lastContentOffset];
I have a uiscrollview and I put 4 uiviews to make an option. I put label 1 for uiview1 and label 2 for uiview2, for uiview 3 i put label 3 and uiview4 i put label 4.
After that I hide the 4 uiview so everytime I pulldown the screen the 4 uiviews will display base on how far the user pull.
Can anyone give an example on how to display the which uiview is selected, when you pull the screen in ios iphone?
You can use contentOffset of scroll View to find how far user pulled scroll View.
Use scroll View delegate Method:
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
By checking frames of your views & scrollView contentOffset, you can determine which view is visible (i.e. uiview1, uiview2,..).
Edit:
Your logic should be like this:
if(uiview1.frame.origin.y - scrollView.contentOffset.y > uiview1.frame.origin.y + uiview1.frame.size.height)
{
//your logic for selection of uiview1
}else if.......
Apply this conditions from last view to first view.
For your reference, you can see a sample implementation of a page control from here. http://developer.apple.com/library/ios/#samplecode/PageControl/
For the implementation you want, to your surprise, the scrollview's width is actually smaller than 320 (or 480). The magic property to set is:
scrollView.clipsToBounds = NO
The only problem with this implementation is that the scrollview get no touch events if the touch is outside the bounds of the scrollView. This can be fix by passing its parent hitTest event to scrollView.
Just to link to to a better explanation: UIScrollView horizontal paging like Mobile Safari tabs
Slightly different from what I recommend but does the same thing.
Edit:
I have a small project called LXPagingViews that does the above, hopefully in an out of the box manner (Do drop me a pull request or feedback in issue): https://github.com/lxcid/LXPagingViews