Refreshing UIScrollView / Animation, timing (Objective-C) - iphone

I have a UIScrollView that is displaying a list of data.
Right now, when the user adds one more item to the list, I can extend the content size of the UIScrollView and scroll down smoothly using setContentOffset and YES to animated.
When the user removes an item from the list, I want to resize the content size of the UIScrollView, and scroll back up one step also in an animated fashion.
How can I get the ordering right? Right now, if I resize the content size before scrolling back up, the scroll isn't animated.
I tried scrolling back up before resizing the content size, but that still didn't give a smooth transition.
Is there a way to finish the scrolling animation BEFORE resizing the content size?
Thanks

Yes, you need to call the method to resize your content after the scroll view's animation is complete. You can do that in the scroll view delegate's scrollViewDidEndScrollingAnimation: method. Set your controller as the delegate, and implement that method.
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
[scrollView setContentSize: newSize];
}
You don't need a timer or a table view for this.

I think you should use UITablView not UIScrollView.
in UITableView you can implement refresh functionality.
using the one line code:
[tblView reloadData];

One thing you can do is to use a timer. As I understand, you want to do this:
Scroll to new viewport
Remove item from list
Resize content size
You could do it like this:
[scrollView setContentOffset:... animated:YES];
[NSTimer scheduledTimerWithTimeInterval:SECONDS_AS_FLOAT target:self selector:#selector(timerCallback:) userinfo:nil repeats:NO];
- (void)timerCallback:(NSTimer *)timer {
// remove item from list at this point
...
// set new content size
[scrollView setContentSize];
}
You have to experiment to see what is the right SECONDS_AS_FLOAT time to use, it should be slightly more than the scroll duration. It will be on the order of a few hundred milliseconds, so you can experiment with a value between say 0.2 and 0.5.

Related

Is there a way to change content offset via Core Animation on UIScrollView with reusing views?

EDIT: I got some progress, so I have simplified and narrowed the problem: I have created new question, which is more clearer and more understandable I hope
I have very large horizontally scrolling UIScrollView, which is reusing its subviews. When user scrolls left, the rightmost view is moved to the leftmost position and its content is updated. Similarly when user scrolls right, the leftmost view is moved to the rightmost position and its content is updated. Similarly like in UITableView. This works nicely using scrollviewDidScroll: delegate method.
Now I need to change offset programatically, but with custom animation behaviour (inertia and bounce back to the final position).
To prototype this, I use
[UIView animateWithDuration:contentOffsetCADuration
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^{
[self setContentOffsetNoDelegateCalls:contentOffset animated:NO];
}
completion:^(BOOL finish){
dispatch_async(dispatch_get_main_queue(), ^{
[timer invalidate];
});
}];
where I animate content offset with disabled delegate callback, so that views are not messed during animation. But I need something to simultaneously reuse and reposition them.
My solution is to create timer and call scrollViewDidScroll: manually so that reusing works as usual, parallel to animation. But it simply does not work - nothing is reused and scrollview's content after animation is in the same state as before animation -> formerly visible pages are moved out of screen.
So the question narrows down how to move subviews and update their content during animation?
Or maybe there might be completely different way doing this ...
Check my answer over here.
Edit: lol, just noticed it's the same author
you may try to change scroll offset in a methode then you can call it from
[self performSelector:<#(SEL)#> withObject:<#(id)#> afterDelay:<#(NSTimeInterval)#>];
then make it stop when it reaches a specific value

How to scroll with UIScrollView with stopping only at pecific regular positions?

I have image that needs to be scrolled in uiscrollview but scrolling needs to be stopped only at specific points. Imagine I have long image of uitableview so I need to scroll it. What I'm thinking about: is it possible to stop scroll only at points where full row is visible at the top? If yes, then how to do it?
You need to set the coordinates to where you want the scroll to move. This is done here -
[scrollView scrollRectToVisible:CGRectMake(0, 50, 1536, 939) animated:NO];
So you need to specify the exact CGRect specs you need. But this will work for you.
If you turn animated:YES then the scroll to this new position is smoothly animated.

Scroll programmatically an UITableView with acceleration and deceleration

I'm trying to scroll a UITableView programmatically by tapping a button. It should be the same effect at doing it by hand or even a little bit longer in scrolling time.
First I tried to use scrollToRowAtIndexPath:atScrollPosition:animated: however it too fast and fixed in time. Also it does not have any acceleration and deceleration.
Any ideas how to scroll a UITableView programmatically as it has been scrolled by hand? I was thinking to send some custom touches to it. Do you think it could work?
Any ideas and help are greatly appreciated! Thanks!
UITableview is a subclass of UIScrollview. Like a UIScrollview, you can programmatically set UITableview's scroll position by setting the contentOffset property.
You change this property value inside an animation block so you have more control in the timing. You'll need to calculate the Y point position. Say you want to scroll to the 50th row, it might be 50 * rowHeight (assuming it's uniform height).
For example:
CGFloat calculatedPosY = 50 * rowHeight // do you own calculation of where you'd like it.
[UIView animateWithDuration:0.2
animations:^{theTableview.contentOffset = CGPointMake(0.0, calculatedPosY);}
completion:^(BOOL finished){ }];
There's another method you could use that provides more options as well
Take a look at this method:
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
Set your button in cell subview and use this code to scroll table programmatically on touch event of button. And if you are not setting button as a subview of table cell then modify this code as needed.
CGPoint point = [button.superview convertPoint:button.frame.origin toView:self.tableView];
CGPoint contentOffset = self.tableView.contentOffset;
contentOffset.y += (point.y - contentOffset.y - self.navigationController.navigationBar.frame.size.height); // Adjust this value as you need
[self.tableView setContentOffset:contentOffset animated:YES];
Well, these are just a couple of "off the top of my head answers," but you could try using this method (in UIScrollView, of which UITableView is a subclass):
-(void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
Once -(CGRect) rectForRowAtIndexPath: gives you the coordinates of the cell, you can then animate to the location of cell. But rather than calling it once, call it a couple of times with a smaller offset, then with a larger and larger offsets and then a smaller ones again. I am worried that it will be difficult to make it look smooth, though.
Still, if that doesn't work, perhaps changing contentOffset value inside of a UIView animateWithDuration:delay:options:animations:completion block would work. Maybe if you use the UIViewAnimationOptionCurveEaseInOut, you can get the effect you want just calling it once. If not, run several animations each part of way and covering great and greater distances before slowing down.

Can a UIScrollView be adapted to zoom horizontally but not vertically? (how)

I'm trying to make a timeline view that shows events as UIButtons that can be pressed for more info.
Ideally I want to lay out the buttons on a UIScrollView, and allow them to stretch/shrink horizontally, but not vertically.
What I'm trying to accomplish would basically be equivalent to using pinch gestures to resize the content view, and using a scroll view to move side to side - could you point me in the right direction, or suggest how you might accomplish something like this?
My first thought is a UIScrollView to scale a UIView subview, and have the UIView subview contain another subview that does not resize vertically, but only does horizontally when bounds change, and disable vertical scrolling. Maybe I could skip one of these UIView subviews and have only one do everything. It just feels like I'm trying to hack this together like an HTML page or something with all these containers.
Not sure if I've explained any of this well enough to hope for an answer, any help is appreciated though.
I've implemented horizontal-only scaling by creating a subclass of UIView that simply overrides setTransform and sets the Y scale to 1 whenever the UIScrollView changes the scale:
#interface DiagramStripView : UIView { }
#end
#implementation DiagramStripView
- (void)setTransform:(CGAffineTransform)newValue;
{
newValue.d = 1.0;
[super setTransform:newValue];
}
#end
As the class name suggests, my view holds a series of diagrams that are each one screen wide. When the user lets go, the view controller resets the view's scale to 1 and redraws everything to the new scale:
- (void) scrollViewDidEndZooming:(UIScrollView *)scrollView
withView:(UIView *)view atScale:(float)scale
{
diagramStripView.transform = CGAffineTransformIdentity;
[self redrawDiagrams:scale];
}
Haven't tried this, but you could try placing everything in a UIScrollView, and whenever you detect a zoom level change, adjust all of the child view transforms to CGAffineTransformMakeScale(1.0, 1.0/zoomLevel). This way, the scrollview is zooming everything up, but each subview is scaling themselves vertically downward, canceling out the zoom.
Assuming you don't actually want to stretch/shrink the button text, the easiest way is to resize all the buttons. I know, it's a pain.

UIScrollView not showing scroll indicator

I have a UIScrollView which I create and size dynamically using...
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width , length);
I then add subviews to the UIScrollView.
I do have scrollView.showsVerticalScrollIndicator = YES;
When scrolling the scroll indicator never appears.
Even if I call [scrollView flashScrollIndicators] nothing happens.
Ideas?
Had the same problem and couldn't find the cause. The last answer gave the final hint: whenever I added new subviews I first removed all existing subviews from the scrollview (and apparently also the scroll indicators).
After checking in my loop, if the subview really was of the kind I wanted to be removed the scroll indicators showed up:
for (NSObject * subview in [[[scrollView subviews] copy] autorelease]) {
if ([subview isKindOfClass:[MySubView class]]) {
[(MySubView*)subview removeFromSuperview];
}
}
Update: changed code to Nikolai's suggestion
When I've dealt with this before, in my implementation of a grid, I would occasionally get some cells over the top of the scroll indicator. To fix this I am now inserting subviews at index 0 rather than adding them, which adds them to the top. So try something like this:
[scrollview insertSubview:subview atIndex:0];
For me, the horizontal indicator had mysteriously disappeared in my app on iOS 7. Later found out that for some strange reason, I had to enable both Shows Horizontal Indicator and Shows Vertical Indicator to make the horizontal one show up. If I set it to not show the vertical indicator, it would also not show horizontal indicator.
I fix this by adding this code after add new subview:
self.showsVerticalScrollIndicator = NO;
self.showsVerticalScrollIndicator = YES;
It will also happen (at least in the case of a UITableView) if the contentSize is too small for the table view to scroll. If you have enabled bouncing, then the tableview does not actually scroll and does not display the indicators therefore. Try fitting more content inside.
It can happen also if the parent of the scrollview is smaller horizontally than the scroll view itself :
The scroll bar is stuck to the right side of the ScrollView / TableView and this right side is not visible due to the parent bounds ( with a clipToBounds hidding it for instance).
I've seen this issue so I share it in case it can help.
Just check the width of your ScrollView's frame not to be bigger than the width of its parent view frame.
Two conditions,
If you are using a storyboard
If you are using a UITableView inside a UIViewController
Then, you should check your indicator insets are set to 0 (or any other number that is relevant to your autolayout):
Noticed this when the UIScrollView was a 48 px tall horizontal band, scrollable horizontally. Maybe Cocoa decides the area is too small for a scroll indicator...