출처 : https://github.com/leah/PullToRefresh

소스 분석하면서 직접 구현해 본것.
나중에 사용하기 위해서 저장

// viewcontroller.h
@interface ViewController : UIViewController <UITableViewDatasource, UITableViewDelegate>
{
    NSMutableArray *data;
    BOOL           bLoading;
    BOOL           bDragging;
    
    UIView *headerView;
    UIActivityIndicatorView *activity;
    UILabel *label;
    UIImageView *imageView;
    NSString *strBeginDragging;
    NSString *strEndDragging;
    NSString *strLoading;
}

@property (nonatomic, retain) NSMutableArray *data;
@property (nonatomic, assign, getter = isLoading ) BOOL bLoading;
@property (nonatomic, assign, getter = isDraggin ) BOOL bDragging;
@property (nonatomic, retain) UIActivityIndicatorView *activity;
@property (nonatomic, retain) UIView *headerView;
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) NSString *strBeginDragging;
@property (nonatomic, retain) NSString *strEndDragging;
@property (nonatomic, retain) NSString *strLoading;

- (void)createHeader;

@end
 
// viewcontroller.m
#define REFRESH_HEADER_HEIGHT 60.0f

@implementation ViewController

@synthesize data;
@synthesize bLoading, bDragging;
@synthesize activity, headerView;
@synthesize label;
@synthesize imageView;
@synthesize strBeginDragging, strEndDragging, strLoading;

#pragma mark - animation
- (void)refresh {
    // This is just a demo. Override this method with your custom reload action.
    // Don't forget to call stopLoading at the end.
    [self performSelector:@selector(stopLoading) withObject:nil afterDelay:2.0];
}

- (void)startLoading {
    self.bLoading = true;
    
    // Show the header
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    UITableView *tableView = (UITableView *)self.view;
    tableView.contentInset = UIEdgeInsetsMake(REFRESH_HEADER_HEIGHT, 0, 0, 0);
    
    label.text = strLoading;
    [activity startAnimating];
    
    
    [UIView commitAnimations];  
    
    // Refresh action!
    [self refresh];
}

- (void)stopLoading {
    self.bLoading = false;
    
    // Hide the header
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDidStopSelector:@selector(stopLoadingComplete:finished:context:)];
    UITableView *tableView = (UITableView *)self.view;
    tableView.contentInset = UIEdgeInsetsZero;
    //[refreshArrow layer].transform = CATransform3DMakeRotation(M_PI * 2, 0, 0, 1);

    
    [self.activity stopAnimating];
    
    [UIView commitAnimations];
    

}

- (void)stopLoadingComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    // Reset the header
    label.text = strBeginDragging;
    label.text = strBeginDragging;
    [imageView layer].transform = CATransform3DMakeRotation(M_PI * 2, 0, 0, 1);
}


#pragma mark - scroll events
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 
{
    if ( self.isLoading ) return;
    
    self.bDragging = true;

}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{
    if ( self.isLoading ) 
    {
        //NSLog(@"isLoading : %f", scrollView.contentOffset.y );        
    }
    else if ( self.isDraggin && scrollView.contentOffset.y < 0 ) 
    {
        //NSLog(@"self.isDragging ; %f", scrollView.contentOffset.y ); 
        [UIView beginAnimations:nil context:NULL];
        if (scrollView.contentOffset.y < -REFRESH_HEADER_HEIGHT) {
            // User is scrolling above the header
            label.text = strEndDragging;
            [imageView layer].transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
        } else { // User is scrolling somewhere within the header
            label.text = strBeginDragging;
            [imageView layer].transform = CATransform3DMakeRotation(M_PI * 2, 0, 0, 1);
        }
        [UIView commitAnimations];        
    }
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate 
{  
    if ( self.isLoading ) return;    
    self.bDragging = false;
    
    NSLog(@"scrollView.contentOffset.y: %f <= %f", scrollView.contentOffset.y, -REFRESH_HEADER_HEIGHT);
    if (scrollView.contentOffset.y <= -REFRESH_HEADER_HEIGHT) {
        // Released above the header
        [self startLoading];
    } 
}

#pragma mark - dealloc
- (void)createHeader
{
    strBeginDragging = [[NSString alloc] initWithString:@"새글을 불러오시겠습니까?"];
    strEndDragging   = [[NSString alloc] initWithString:@"놓으면 새글을 불러옵니다."];
    strLoading       = [[NSString alloc] initWithString:@"로딩중입니다..."];
    
    
    // view
    headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0-REFRESH_HEADER_HEIGHT, 320, REFRESH_HEADER_HEIGHT)];
    
    // activityindicator
    activity = [[UIActivityIndicatorView alloc] init];
    activity.center = CGPointMake(320.f/2, REFRESH_HEADER_HEIGHT/2);
    activity.hidesWhenStopped = true;
    activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    
    // label
    label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, REFRESH_HEADER_HEIGHT)];
    label.text = strBeginDragging;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:12.0];
    label.textAlignment = UITextAlignmentCenter;
    
    // imageView
    UIImage *image = [UIImage imageNamed:@"arrow.png"];
    imageView = [[UIImageView alloc] initWithImage:image];
    imageView.frame = CGRectMake(0
                                 , (REFRESH_HEADER_HEIGHT-image.size.height) /2
                                 , image.size.width
                                 , image.size.height);
    imageView.backgroundColor = [UIColor clearColor];
    
    [headerView addSubview:activity];
    [headerView addSubview:label];
    [headerView addSubview:imageView];
    
    [self.view addSubview:headerView];
}


- (void)dealloc
{
    [strLoading release];
    [strBeginDragging release];
    [strEndDragging release];
    [imageView release];
    [label release];
    [activity release];
    [headerView release];
    [data release];
    
    [super dealloc];
}

#pragma mark - table datasource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    
    cell.textLabel.text = [data objectAtIndex:indexPath.row];
    
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [data count];
}

#pragma mark - memorywarning


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.data = [NSMutableArray arrayWithObjects:
                 @"Pull down to refresh", nil];
    
    [self createHeader];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

 
저작자 표시
신고
posted by 뚱2