Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use a UIRefreshControl in a UIScrollView?

I have about 5 UIScrollView's already in my app which all load multiple .xib files. We now want to use a UIRefreshControl. They are built to be used with UITableViewControllers (per UIRefreshControl class reference). I do not want to re-do how all 5 UIScrollView work. I have already tried to use the UIRefreshControl in my UIScrollView's, and it works as expected except for a few things.

  1. Just after the refresh image turns into the loader, the UIScrollView jumps down about 10 pixels, which only does not happen when I am very careful to drag the UIScrollview down very slowly.

  2. When I scroll down and initiate the reload, then let go of the UIScrollView, the UIScrollView stays where I let it go. After it is finished reloading, the UIScrollView jumps up to the top with no animation.

Here is my code:

-(void)viewDidLoad {       UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];       [refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];       [myScrollView addSubview:refreshControl]; }  -(void)handleRefresh:(UIRefreshControl *)refresh {       // Reload my data       [refresh endRefreshing]; } 

Is there any way I can save a bunch of time and use a UIRefreshControl in a UIScrollView?

Thank You!!!

like image 759
SirRupertIII Avatar asked Feb 16 '13 00:02

SirRupertIII


People also ask

What is scroll view in Swift?

Overview. UIScrollView is the superclass of several UIKit classes, including UITableView and UITextView . A scroll view is a view with an origin that's adjustable over the content view. It clips the content to its frame, which generally (but not necessarily) coincides with that of the app's main window.


1 Answers

I got a UIRefreshControl to work with a UIScrollView:

- (void)viewDidLoad {     UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];     scrollView.userInteractionEnabled = TRUE;     scrollView.scrollEnabled = TRUE;     scrollView.backgroundColor = [UIColor whiteColor];     scrollView.contentSize = CGSizeMake(500, 1000);      UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];     [refreshControl addTarget:self action:@selector(testRefresh:) forControlEvents:UIControlEventValueChanged];     [scrollView addSubview:refreshControl];      [self.view addSubview:scrollView]; }  - (void)testRefresh:(UIRefreshControl *)refreshControl {         refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Refreshing data..."];          dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{          [NSThread sleepForTimeInterval:3];//for 3 seconds, prevent scrollview from bouncing back down (which would cover up the refresh view immediately and stop the user from even seeing the refresh text / animation)          dispatch_async(dispatch_get_main_queue(), ^{             NSDateFormatter *formatter = [[NSDateFormatter alloc] init];             [formatter setDateFormat:@"MMM d, h:mm a"];             NSString *lastUpdate = [NSString stringWithFormat:@"Last updated on %@", [formatter stringFromDate:[NSDate date]]];              refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastUpdate];              [refreshControl endRefreshing];              NSLog(@"refresh end");         });     }); } 

Need to do the data update on a separate thread or it will lock up the main thread (which the UI uses to update the UI). So while the main thread is busy updating the data, the UI is also locked up or frozen and you never see the smooth animations or spinner.

EDIT: ok, I'm doing the same thing as OP and i've now added some text to it (ie, "Pull to Refresh") and it does need to get back onto the main thread to update that text.

Updated answer.

like image 118
Padin215 Avatar answered Oct 25 '22 02:10

Padin215