I was wondering how one would update items such as a UILabel when a scroll view reaches a certain point - I have a picker app in which the user scrolls a UIScrollView and the contents of the screen update relative to the position of the UIScrollView however I would like to know how to actually update the contents of a view, again, things like a UILabel relative to the position of the UIScrollView.
You can use scrollView.contentOffset to get current content position inside scrollView.
Related
I built a project that has a mapView (Google Maps), tableView and a UIScrollView.
The tableView is on the bottom and when the app starts you see a really small part of it and the most of it is out of the screen (on the bottom). When the user scrolls the tableView is revealed and going up from the bottom until it covers all of the screen. It's all working but I have one small problem: I want that the user will be able to touch the map (zoom, move, etc.) but the UIScrollView is on top of the map and when the user tries to interact with the map the scrollView detects a scroll and the map doesn't get a touch event. Is there any way that the UIScrollView wouldn't detect the touch on the area of the map? The scrollView should cover all of the screen for the tableView detection (or maybe not and there is another way?) and I don't want the map to be a subclass of the scrollView because it will go up with the table when the user scrolls (I want the tableView to go on top of the map and not with it).
Thanks!
Going off of my comment. You will need to set the top space to superview constraint on the scroll view. Create an IBOutlet for the constraint and a float variable for the value of the constraint and also connect it to your top space to superview constraint that you will create in interface builder on the scrollview.
in your .h:
IBOutlet NSLayoutConstraint *topSpace;
float topSpaceValue;
Then update that constraint using this:
topSpace.constant = topSpaceValue;
You can detect scrolling in the scrollview with this method and set the topSpace constraint:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
topSpace.constant = topSpaceValue;
}
Now, the actual logic of detecting how fast the scrollview is moving and what value to set for topSpaceValue is up to you to figure out. Also you will need to detect what direction the scroll view is moving to increase or decrease the value of topSpaceValue.
This looks like the link you need for detecting the speed: iPhone UIScrollView Speed Check
So according to the speed, set the constraint. And you will probably want to check for a minimum value for the constraint, like 0, so the scrollview doesn't scroll off the top of the screen.
UIScrollView has several UILabel subviews, which are set to scroll horizontally (think about Instagram filters scrolling view). I can scroll UIScrollSubview, act upon tapping on a UILabel subview. Next thing I would like to achieve is to programmatically scroll to an invisible UILabel subview, when the users selects the last visible subview on the right (similar to how the instagram filters scroll out of the visible area when the user selects the last visible filter).
When the user touches the last visible UILabel subview, I execute
[_scrollView setContentOffset:CGPointMake(64, 0) animated:YES];
where 64 is the width of every UILabel subview of the scrollView.
This works fine only on the first selection event of the last visible subview. Once the scrollView scrolled to reveal the desired subview on the right side of the selected one, and I select newly revealed subview, the scrollView correctly scrolls to the left revealing the next invisible subview, but then jumps back (to the right) two positions (128). The desired behaviour is to always scroll to the left when the last visible subview on the right is selected (right now this happens only on the first selection).
Any suggestions would be appreciated.
Thanks.
EDIT: The bouncing issue went away in 2 cases:
scrollView.pagingEnabled: NO;
user performs relatively long touches on subviews
You should add 64 to the current contentOffset.
[_scrollView setContentOffset:CGPointMake(_scrollView.contentOffset.x + 64, 0) animated:YES];
I have a UIScrollView which has two pages and only scroll horizontally.
The scrolling and paging is controlled using a UIPageControl. I have placed a UIImageView on the scrollView which contains an image of an iPhone (shown in red in the image below) that says Hello inside page 1.
I wanted to animate the UIImageView to rotate and change its position as shown in the image when the user scrolls from page-1 to page-2. Also the animation should ideally rotate back when the user is scrolling back from page-2 to page-1.
The animation or movement of the UIImageView is based on how much the user is scrolling horizontally and not based on time.
How can I rotate the UIImageView back and forth based on the scroll position of the UIScrollView?
Set a delegate for your scroll view. Probably you want your view controller to be the delegate. You need to add UIScrollViewDelegate to its list of protocols.
Then, in the delegate, implement scrollViewDidScroll:. In scrollViewDidScroll:, look at the scroll view's contentOffset. Based on the contentOffset, set the image view's transform and center to rotate and move it where you want.
To find by how much the scroll view has scrolled, you can check UIScrollView's contentOffset property:
contentOffset - The point at which the origin of the content view is
offset from the origin of the scroll view.
Then to rotate that image view, you could do this:
self.imageview.transform = CGAffineTransformMakeRotation(M_PI/2);
As far as the animation goes, I personally don't have much experience with it. But you could have a look at Simple Animation Using UIImageView.
UIScroll view lets me declared a scrollable area when I have too much content for one page. Strangely, the scroll view in question behaves as desired in the X-axis, with no scrolling whatsoever allowed. Unfortunately, the Y axis -- where scrolling is necessary -- doesn't 'clip' the allowed scroll area to the content size. The user can scroll outside of the content size, and only after they let go does scroll view 'bounce' back to the allowed zone.
I want to prevent the user from scrolling further up than there is content to view (down doesn't bother me) because it looks 'wrong' to have the header at the top of the scroll view pull down, leaving the regular background behind it.
If you are making your UIScrollView in interface this is as simple as deselecting the
"Bounces, Bounces Horizontally, and Bounces Vertically"
check boxes in your scrollView's attributes. If you are designing the UIScrollView in code you can add this.
self.textView.alwaysBounceHorizontal = NO;
self.textView.alwaysBounceVertical = NO;
self.textView.bounces = NO;
i have a scrollView that fits the whole screen. In that View i show some UIImages that scroll horizontally. Like in the PageControl Project from Apple.
Then, when the user taps the screen, i fade in a scrollView at the bottom with some other images. Also horizontally scrolling. Like in the ScrollView Project from Apple.
My problem is, that when i come to the end of the scrollView which was faded in, the upper scrollView also starts to drag. How can i stop that during activation of the second scrollView?
Redraw the frame limits on your first scrollView when the second enters the screen. That way, your touches won't respond to both views. This means you need a container-view to keep both views seperated from each other.
Then you just rescale it back whenever your second scrollView disappears.
Edit: or disable scroll in your first scrollview while the second is open.