Switch
Switch

Reputation: 5376

Refreshing UIScrollView / Animation, timing (Objective-C)

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

Upvotes: 3

Views: 2647

Answers (3)

lucius
lucius

Reputation: 8685

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.

Upvotes: 4

Jaanus
Jaanus

Reputation: 17866

One thing you can do is to use a timer. As I understand, you want to do this:

  1. Scroll to new viewport
  2. Remove item from list
  3. 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.

Upvotes: 0

MohammedYakub M.
MohammedYakub M.

Reputation: 2941

I think you should use UITablView not UIScrollView.

in UITableView you can implement refresh functionality.

using the one line code:

[tblView reloadData];

Upvotes: 1

Related Questions