Im attempting to replicate the resizing behaviour of a screen seen in the iOS app Rdio. The screen in question provides an overview of a selected album and contains a UIView on the top half and a UITableView on the bottom half. When the tableView is scrolled, it first resizes upwards to fill the screen, then begins to scroll through its content normally once the maximum height is reached.
After some searching I found this question: Dragging UITableView which is basically asking for the same thing, however its accepted method is the same as my initial thoughts & trial, which was to use a UIPanGestureRecognizer and resize the tableviews height according to the translation of the pan.
This does not provide the behaviour i'm looking for. Using this method only allows you to statically drag the tableviews height up or down and it has the added issue of the panGesture overriding that of the tableViews which then prevents scrolling through the content.
The resizing behaviour of the Rdio app functions and feels exactly like a UIScrollView, it has inertia. You can drag it all the way, flick it up or down, and it smoothly resizes. When the tableView has reached its full-size or original half-size, the remaining inertia is seemingly passed on the tableview causing the cells to scroll as they normally would for that amount. I know they must be manipulating UIScrollViews, I just can't figure out how.
As a final note, eventually I will be using AutoLayout on this screen so i'm wondering how that will potentially hinder or help this situation as well.
Update
This approach has gotten me closest to the behaviour i'm looking for so far.
Flicking the tableView upwards behaves exactly like I wanted it to (resize with inertia & continue scrolling when max height is reached), although with less sensitivity than i'd like. Flicking downwards however, provides no inertia and instantly stops.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect scrollViewFrame = scrollView.frame;
CGFloat scrollViewYOffset = scrollView.contentOffset.y;
if (scrollViewFrame.origin.y <= kTableViewMaxYOrigin || scrollViewFrame.origin.y <= _originalTableViewFrame.origin.y)
{
scrollViewFrame.origin.y -= scrollViewYOffset;
if(scrollViewFrame.origin.y >= kTableViewMaxYOrigin && scrollViewFrame.origin.y <= _originalTableViewFrame.origin.y)
{
scrollViewFrame.size.height += scrollViewYOffset;
scrollView.frame = scrollViewFrame;
scrollView.contentOffset = CGPointZero;
}
}
}
I made a version that uses autolayout instead.
It took me a while of trial and error to get this one right!
Please use the comments and ask me if the answer is unclear.
In viewDidLoad save the initial height of your layout constraint determining the lowest down you want the scrollview to be.
- (void)viewDidLoad
{
...
_initialTopLayoutConstraintHeight = self.topLayoutConstraint.constant;
...
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
BOOL leaveScrollAlone = self.topLayoutConstraint.constant == _initialTopLayoutConstraintHeight && scrollView.contentOffset.y <= 0;
if (leaveScrollAlone)
{
// This allows for bounce when swiping your finger downwards and reaching the stopping point
return;
}
// Do some capping of that layout constraint to keep it from going past the range you want it to be.
// In this case, I use self.topLayoutGuide.length so that my UICollectionView scales all the way until
// it hits the bottom of the navigation bar
CGFloat topLayoutConstraintLength = _initialTopLayoutConstraintHeight - scrollView.contentInset.top;
topLayoutConstraintLength = MAX(topLayoutConstraintLength, self.topLayoutGuide.length);
topLayoutConstraintLength = MIN(topLayoutConstraintLength, _initialTopLayoutConstraintHeight);
self.topLayoutConstraint.constant = topLayoutConstraintLength;
// Keep content seemingly still while the UICollectionView resizes
if (topLayoutConstraintLength > self.topLayoutGuide.length)
{
scrollView.contentInset = UIEdgeInsetsMake(scrollView.contentInset.top + scrollView.contentOffset.y,
scrollView.contentInset.left,
scrollView.contentInset.bottom,
scrollView.contentInset.right);
scrollView.contentOffset = CGPointZero;
}
// This helps get rid of the extraneous contentInset.top we accumulated for keeping
// the content static while the UICollectionView resizes
if (scrollView.contentOffset.y < 0)
{
self.topLayoutConstraint.constant -= scrollView.contentOffset.y;
scrollView.contentInset = UIEdgeInsetsMake(scrollView.contentInset.top + scrollView.contentOffset.y,
scrollView.contentInset.left,
scrollView.contentInset.bottom,
scrollView.contentInset.right);
}
// Prevents strange jittery artifacts
[self.view layoutIfNeeded];
}
It turns out the key component to getting the smooth inertial resizing in both directions was to update the scrollViews contentInset.top by its contentOffset.y.
I believe this makes sense in retrospect as if the content within is already at the top it cannot scroll anymore, hence the sudden stop rather than smooth scroll. At least thats my understanding.
Another key point was to make sure the cells only started scrolling once maximum or original height was achieved. This was done simply by setting the scrollViews contentOffset to CGPointZero each time the view resized until maximum or original height was reached.
Here is the - (void)scrollViewDidScroll:(UIScrollView *)scrollView method demonstrating how to achieve this effect.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect scrollViewFrame = scrollView.frame;
CGFloat scrollViewTopContentInset = scrollView.contentInset.top;
CGFloat scrollViewYOffset = scrollView.contentOffset.y;
if (scrollViewFrame.origin.y <= kTableViewMaxYOrigin || scrollViewFrame.origin.y <= _originalTableViewFrame.origin.y)
{
scrollViewFrame.origin.y -= scrollViewYOffset;
if(scrollViewFrame.origin.y >= kTableViewMaxYOrigin && scrollViewFrame.origin.y <= _originalTableViewFrame.origin.y)
{
scrollViewFrame.size.height += scrollViewYOffset;
scrollViewTopContentInset += scrollViewYOffset;
scrollView.frame = scrollViewFrame;
scrollView.contentInset = UIEdgeInsetsMake(scrollViewTopContentInset, 0, 0, 0);
scrollView.contentOffset = CGPointZero;
}
}
}
I haven't seen the app in question, but from your description... in your tableView's delegate method -scrollViewDidScroll:, set tableView.frame.origin.y to albumView.frame.height - tableView.contentOffset.y and change its height accordingly.
(If you're using autolayout, you'll have to change the constraints pertaining to the tableView's frame rather than the frame itself.)
Related
I'd like to implement a "zoom" effect on a paging UIScrollView that I've created, but I am having a lot of difficulty. My goal is that as a user begins to scroll to the next page, the current page zooms out to become a little bit smaller. As the next page comes into view, it zooms in until it becomes its full size. The closest thing I could find to an example was this...
https://www.pinterest.com/pin/147141112804210631/
Can anyone give me some pointers on how to accomplish this? I've been banging my head against a wall for the last 3 days on this.
I would recommend using the scrollView.contentOffset.y of your paginated UIScrollView to keep track of the scroll and to use that value to animate the transform of your views inside the UIScrollView.
So add your paginated scrollview and make self as delegate.
paginatedScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [[self view] bounds].size.width, [[self view] bounds].size.height-paginatedScrollViewYOffset)];
[self.view addSubview:paginatedScrollView];
paginatedScrollView.pagingEnabled = YES;
[paginatedScrollView setShowsVerticalScrollIndicator:NO];
[paginatedScrollView setShowsHorizontalScrollIndicator:NO];
[paginatedScrollView setAlwaysBounceHorizontal:NO];
[paginatedScrollView setAlwaysBounceVertical:YES];
paginatedScrollView.backgroundColor = [UIColor clearColor];
paginatedScrollView.contentSize = CGSizeMake([[self view] bounds].size.width, [[self view] bounds].size.height*2); //this must be the appropriate size depending of the number of pages you want to scroll
paginatedScrollView.delegate = self;
Then use the delegate method scrollViewDidScroll to keep track of the scrollView.contentOffset.y
- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(#"Scroll Content Offset Y: %f",scrollView.contentOffset.y);
//use here scrollView.contentOffset.y as multiplier with view.transform = CGAffineTransformMakeScale(0,0) or with view.frame to animate the zoom effect
}
Use this Code scrollview its zoom in when scroll next page, the code is given below,
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
GridCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"CollectCell" forIndexPath:indexPath];
cell.myscrollview.minimumZoomScale = 5.0;
cell.myscrollview.zoomScale = 5.0;
cell.myscrollview.contentSize = cell.contentView.bounds.size;
return cell;
}
if you change the zoom scale value its automatically zoom in or zoom out to be showed when scroll next or previous page.
hope its helpful.
I actually just posted an answer to a very similar question, where somebody tried to achieve this effect using a UICollectionView. The link to my answer is here: https://stackoverflow.com/a/36710965/3723434
Relevant piece of code I will post here:
So another approach would be to to set a CGAffineTransformMakeScale( , ) in the UIScrollViewDidScroll where you dynamically update the pages' size based on their distance from the center of the screen.
For every page, calculate the distance of its center to the center of yourScrollView
The center of yourScrollView can be found using this nifty method: CGPoint point = [self.view convertPoint:yourScrollView.center toView:*yourScrollView];
Now set up a rule, that if the page's center is further than x away, the size of the page is for example the 'normal size', call it 1. and the closer it gets to the center, the closer it gets to twice the normal size, 2.
then you can use the following if/else idea:
if (distance > x) {
page.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
} else if (distance <= x) {
float scale = MIN(distance/x) * 2.0f;
page.transform = CGAffineTransformMakeScale(scale, scale);
}
What happens is that the page's size will exactly follow your touch. Let me know if you have any more questions as I'm writing most of this out of the top of my head).
I've done some work on stylized app guide page before.
For Me, I would use CADisplayLink to track the contentOffset.x of the scrollView, associate the value with your animation process. Don't put your views on the scrollView, put them on an overlay view of this scrollView.
This solution follows the philosophy: Fake it before you make it.
Based on CADisplayLink and physics simulation of UIScrollView, you will get smooth animation. Believe me.
What you really want isn't a UIScrollView, it's a UICollectionView with a custom layout. UICollectionViewLayoutAttributes has a transform property that you can set.
Say for example, in layoutAttributesForElementsInRect::
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let attributes = super.layoutAttributesForElementsInRect(rect) else {
return nil
}
return attributes.map { attribute -> UICollectionViewLayoutAttributes in
if attribute.frame.origin.y < 0 {
let scale = -attribute.frame.origin.y / attribute.frame.height
attribute.transform = CGAffineTransformMakeScale(scale, scale)
}
return attribute
}
}
Here, you're filtering by if the element is on the screen (so non-visible elements won't be counted) and checking to see if the y offset is less than 0. If it is, you take the difference between the negated y value and the item's height and turn that into a proportional scale.
You can do it however you want, if you want the scale to be between 1 and 0.5 for example. I like this way of doing things over mucking around with a scroll view.
I've been working at this for a good 48 hours now and can't seem to solve it on my own. What I'm trying to achieve is when a UITableViewCell is scrolled from the bottom to the top of the screen it starts small, magnifies as it reaches the center portion of the screen, and then begins to reduce in size again as it scrolls up and off of the screen (and vice versa, top to bottom). I can kind of get this to work, but it seems as though the contentView is resizing, the actual cell height (of the UITableView) is not.
What I want:
Scrolling up or down smoothly adjusts the size of all the uitableviewcells based on its y position on the screen.
What appears to be happening (though I'm having trouble confirming):
Again, it's as if the cell height is not being adjusted, but the height of the contentView is.
I can achieve this by overriding the layoutVisibleCells method of the UITableView:
func layoutVisibleCells() {
let indexpaths = indexPathsForVisibleRows!
let totalVisibleCells = indexpaths.count - 1
if totalVisibleCells <= 0 { return }
for index in 0...totalVisibleCells {
let indexPath = indexpaths[index]
if let cell = cellForRowAtIndexPath(indexPath) {
var frame = cell.frame
if let superView = superview {
let point = convertPoint(frame.origin, toView:superView)
let pointScale = point.y / CGFloat(superView.bounds.size.height)
var height = frame.size.height + point.x;
if height < 150.0 {
height = 150.0;
}
if height > 200.0 {
height = 200.0;
}
frame.size.height = height;
NSLog("index %li: x: %f", index,frame.origin.x);
NSLog("point at index %li: %f, %f - percentage: %f", index,point.x,point.y, pointScale);
}
cell.frame = frame
}
}
}
I took the idea for making the adjustment in this method from COBezierTableView, which is written in Swift. The rest of my project is in Obj-C, so deciphering things was a bit challenging, as I'm still learning my way there.
Any insight on my current attempt, or suggestions for how to achieve this in another, completely different way, are totally welcome. Thanks in advance.
UPDATE
So I've actually found that the opposite is happening of what I suspected. The UITableViewCell frame IS being resized correctly. It's the cell's contentView that is not resizing fully. It does resize partially, but for some reason it does not resize to match the cell's frame. Not sure why ...
I have a UIScrollView which holds a custom drawing view. The drawing view is used to draw a large content (10000x10000 pixels). Since I cannot embed huge view inside scrollview [due memory limitations], I have created a custom view which is almost twice the size of the scrollView. I have used the StreetScroller [apple sample] logic to implement the same. This works fine but there is a problem with identifying touches. You can download the updated sample from here https://github.com/praveencastelino/SampleApps/tree/master/StreetScroller
Since the contentOffset of scrollview is reset to center whenever it moves 'X amount of pixels' from its center. Hence, the scrollView content offset is different from what we actually need. This what we do in scrollView.
- (void)recenterIfNecessary
{
CGPoint currentOffset = [self contentOffset];
CGFloat contentHeight = [self contentSize].height;
CGFloat contentWidth = [self contentSize].width;
CGPoint centerOffset,distanceFromCenter;
centerOffset.y = (contentHeight - [self bounds].size.height) / 2.0;
distanceFromCenter.y = fabs(currentOffset.y - centerOffset.y);
centerOffset.x = (contentWidth - [self bounds].size.width) / 2.0;
distanceFromCenter.x = fabs(currentOffset.x - centerOffset.x);
if (distanceFromCenter.y > (contentHeight / 6.0))
{
self.contentOffset = CGPointMake(currentOffset.x, centerOffset.y);
[_labelContainerView didResetByVerticalDistancePoint:CGPointMake(currentOffset.x, centerOffset.y - currentOffset.y) visibleFrame:[self bounds]];
}
if (distanceFromCenter.x > (contentWidth / 6.0))
{
self.contentOffset = CGPointMake(centerOffset.x, currentOffset.y);
[_labelContainerView didResetByHorizontalDistancePoint: CGPointMake(centerOffset.x - currentOffset.x, currentOffset.y) visibleFrame:[self bounds]];
}
}
Whenever scrollview resets the center, the custom view notified and it tracks the virtual content offset.
-(void)didResetByVerticalDistancePoint:(CGPoint)distance visibleFrame:(CGRect)frame
{
_contentOffsetY += distance.y;
NSLog(#"_contentOffsetY %f",_contentOffsetY);
[self setNeedsDisplay];
}
However, I wanted to calculate the virtual content offset whenever scroll view scrolls [At present the content offset is calculated only when we reset content offset of scrollview to center]. This would eventually help me in handling touches.
Also, I need a way to restrict the bounds of the scrollview from scrolling infinitely. I want to display only the content and avoid the scrolling if user tries to scroll it further.
I want to change the height of my UITableView based on the scrolled content. Right now I do it by getting the scrollViewDidScroll event and then getting scrolled value and then change the height. Here is my code (for simplification I omitted the irrelevant code):
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
float currentOffset = scrollView.contentOffset.y
double delta = currentOffset - storedOffset;
if(delta != 0)
{
delta = abs(delta);
CGRect newListFrame = myTableView.frame;
float newListHeight = newListFrame.size.height + delta;
newListFrame.size.height = newListHeight;
myTableView.frame = newListFrame;
}
storedOffset = currentOffset;
}
But this approach is wrong because with this approach my UITableView's content is scrolled only a little bit and that's not what I want. I just want to get the value of that list that would be scrolled without actually scrolling it. Is there any way to do that? I thing I could get raw finger moved event but can I get it on UITableVIew? Can I do something like this using a UITableView method?
I have spent the past 12 hours futzing with this and I'm going braindead.
view on bitbucket:
https://bitbucket.org/burneraccount/scrollviewexample
git https: https://bitbucket.org/burneraccount/scrollviewexample.git
git ssh: git#bitbucket.org:burneraccount/scrollviewexample.git
^ That is a condensed, self-contained Xcode project that exemplifies the problem I'm having in my real work.
Summary
I am trying to achieve a static-image effect, where the image (a rock in the example) appears stuck to the screen while the lower content scrolls upwards appearing to go above the scrollview.
There's a screen-sized (UIScrollView*)mainScrollView. This scrollview has a (UIView*)contentView. contentView is ~1200 pts long and has two subviews: (UIScrollView*)imageScroller and (UITextView*)textView.
All works well until you scroll up a little bit, then scroll down really fast. The resulting position after movement stops is incorrect. It somehow doesn't update properly in the scrollviewDidScroll delegate method. The fast downward scroll (only after you've dragged up) behaves somewhat correctly, but still results in a misplaced view:
gently dragging does almost nothing, and the velocity of the scroll is directly related to the incorrect offset.
Here is the offending code:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView == self.mainScrollView)
{
CGFloat mainOffset = self.mainScrollView.contentOffset.y;
if (mainOffset > 0 && mainOffset < self.initialImageScrollerFrame.size.height)
{
CGRect imageFrame = self.imageScroller.frame;
CGFloat offset = mainOffset-self.lastOffset;
self.imageScroller.frame = CGRectMake(imageFrame.origin.x,
imageFrame.origin.y + offset, // scroll up
imageFrame.size.width,
imageFrame.size.height - offset); // hide the bottom of the image
self.lastOffset = mainOffset;
}
}
}
I've restructured this in almost every way I could imagine and it always has similar effects. The worst part is how the very simple and straightforward code fails to do what is seems like it's guaranteed to do.
Also odd is that the zoom-effect I use in my real project works fine using the same mechanism to size the same view element. It runs inside (contentOffset < 0) instead of (contentOffset > 0), so it only zoom in on the imageScroller when you're pulling the view below it's normal offset. That leads me to conspire that some data is lost as it crosses the contentOffset 0, but my conspiracies have been shot down all day.
This is example is a lot less complicated than the real project I'm working on, but it properly reproduces the problem. Please let me know if you can't open my project, or can't connect to the repo.
There is likely a largely obvious fix for this, but I am hours past the point of having any new perspective. I will be thoroughly amazed if I ever get this to work.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView == self.mainScrollView)
{
CGFloat mainOffset = self.mainScrollView.contentOffset.y;
if (mainOffset >= 0 && mainOffset < self.initialImageScrollerFrame.size.height)
{
CGRect imageFrame = self.imageScroller.frame;
CGFloat offset = self.mainScrollView.contentOffset.y-self.lastOffset;
if ( imageFrame.origin.y + offset > 0) { //[2]
self.imageScroller.frame = CGRectMake(imageFrame.origin.x,
imageFrame.origin.y + offset,
imageFrame.size.width,
imageFrame.size.height - offset);
self.lastOffset = mainOffset;
}
}else if (mainOffset < 0) { //[1]
self.imageScroller.frame = self.initialImageScrollerFrame;
}
}
}
[1] - When your mainOffset goes below zero, you need to reset your imageScroller frame. ScrollViewDidScroll does not register with every pixel's-worth of movement, it is only called every so often (try logging it, you will see). So as you scroll faster, it's offsets are further apart. This can result in a positive scroll of say +15 becoming a negative scroll on the next call to scrollViewDiDScroll. So your imageScroller.frame.y may get stuck on that +15 offset even when you expect it to be zero.Thus as soon as you detect a negative value for mainOffset, you need to reset your imageScroller frame.
[2] - similarly here you need to ensure here that your imageScroller.frame.y will always be +ve.
These fixes do the job, but I would still consider them "ugly and not production-worthy". For a cleaner approach you might want to reconsider the interplay between these views, and what you are trying to achieve.
By the way, your 'image.png' is actually a jpg. While these renders fine on the simulator, it will break (with compiler errors) on a device.
I'll post my updates here.
One answerer mentioned - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView and while that doesn't apply here, the similar scrollViewDidEndDecelerating: does. In my struggles yesterday I implemented
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
if (self.mainScrollView.contentOffset.y < 10)
{
[UIView animateWithDuration:0.2 animations:^{
self.imageScroller.frame = self.initialImageScrollerFrame;
}];
}
}
and this is somewhat of a workaround, but it's ugly and definitely not production-worthy. It just animated the frame back to its original position if the contentOffset is close enough to 0. It's a step in the right direction, but I'm afraid it doesn't deserve merit as a solution.
----
Also added
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint *)targetContentOffset
{
CGPoint point = *targetContentOffset;
CGFloat offset = point.y;
if (0 <= offset && offset < 10)
{
[UIView animateWithDuration:0.2 animations:^{
self.imageScroller.frame = self.initialImageScrollerFrame;
}];
}
}
which brings me closer to the intended effect, but still remains unusable for a production application.
The same issue but for add another functionality, button for left/right listing UIScrollView, after fast tapping on it, scroll view is broke my brain ;(
I think better scrolling without animation, it preventing glitches for UIScrollView content. May be it is not answer, but...
Hey mate use the below method that might help you out, its a delegate method of UIScrollView:
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
And within this method you can change the scrollview frame as x and y to 0,0