Refreshing code on UIScrollView UITableView

Source: Internet
Author: User
Since the success of Tweetie, pull-down Refresh has become a method for updating content of almost all apps. If you do not want to fall into the conventional style, you can refer to the following CocoaChina member "diyago" to refresh the code. Below is. h file code // EGORefreshTableHeaderView. h // Demo /// Created by Devin Doty on 10/14/09October14. // Copyright 2009 enormego. all rights reserved. /// modified by: Strong iphone development QQ group: 79190809 mailbox: zhuolaiqiang@gmail.com // original address: http://blog.csdn.net/diyagoanyhacker/archive/2011/05/24/6441805.aspx#import <UIKit/UIKit. h> # import <QuartzCore/QuartzCore. h> typedef enum {EGOOPullRefreshPulling = 0, EGOOPullRefreshNormal, E Response,} EGOPullRefreshState; @ protocol response; @ interface EGORefreshTableHeaderView: UIView {id _ delegate; Response _ state; UILabel * _ lastUpdatedLabel; UILabel * _ statusLabel; CALayer * _ arrowImage; UIActivityIndicatorView * _ activityView;} @ property (nonatomic, assign) id <EGORefreshTableHeaderDelegate> delegate;-(void) refreshLastUpdatedDate;-(void) Rollback :( UIScrollView *) scrollView;-(void) rollback :( UIScrollView *) scrollView; @ end @ protocol EGORefreshTableHeaderDelegate-(void) egoRefreshTableHeaderDidTriggerRefresh :( EGORefreshTableHeaderView *) view;-(BOOL) egoRefreshTableHeaderDataSourceIsLoading :( EGORefreshTableH EaderView *) view; @ optional-(NSDate *) egorefreshtableheader1_celastupdated :( EGORefreshTableHeaderView *) view; @ end the following is. m file code // EGORefreshTableHeaderView. m // Demo // modified by: Strong iphone development QQ group: 79190809 mailbox: zhuolaiqiang@gmail.com // # define RefreshViewHight 65.0f # import "EGORefreshTableHeaderView. h "# define TEXT_COLOR [UIColor colorWithRed: 87.0/255.0 green: 108.0/255.0 blue: 137.0/255.0 alpha: 1.0] # define FL Listen 0.18f @ interface EGORefreshTableHeaderView (Private)-(void) setState :( partial) aState; @ end @ implementation progress @ synthesize delegate = _ delegate;-(id) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {self. autoresizingMask = UIViewAutoresizingFlexibleWidth; self. backgroundColor = [UIColor colorWithRed: 226.0/255.0 green: 2 31.0/255.0 blue: 237.0/255.0 alpha: 1.0]; UILabel * label = [[UILabel alloc] initWithFrame: CGRectMake (0.0f, RefreshViewHight-30366f, self. frame. size. width, 20366f)]; label. autoresizingMask = UIViewAutoresizingFlexibleWidth; label. font = [UIFont systemFontOfSize: 12.0f]; label. textColor = TEXT_COLOR; label. shadowColor = [UIColor colorWithWhite: 0.9f alpha: 1.0f]; label. shadowOffset = CGSizeMake (0.0f, 1.0 F); label. backgroundColor = [UIColor clearColor]; label. textAlignment = custom; [self addSubview: label]; _ lastUpdatedLabel = label; [label release]; label = [[UILabel alloc] initWithFrame: CGRectMake (0.0f, callback-481_f, self. frame. size. width, 20366f)]; label. autoresizingMask = UIViewAutoresizingFlexibleWidth; label. font = [UIFont boldSystemFontOfSize: 13.0f]; label. textColor = TEXT_COLOR; label. shadowColor = [UIColor colorWithWhite: 0.9f alpha: 1.0f]; label. shadowOffset = CGSizeMake (0.0f, 1.0f); label. backgroundColor = [UIColor clearColor]; label. textAlignment = UITextAlignmentCenter; [self addSubview: label]; _ statusLabel = label; [label release]; CALayer * layer = [CALayer layer]; layer. frame = CGRectMake (25366f, RefreshViewHight-RefreshViewHight, 30366f, 55.0f); layer. co NtentsGravity = kCAGravityResizeAspect; layer. contents = (id) [UIImage imageNamed: @ "blueArrow.png"]. CGImage; # if _ IPHONE_ OS _VERSION_MAX_ALLOWED> = 40000 if ([UIScreen mainScreen] respondsToSelector: @ selector (scale)]) {layer. contentsScale = [[UIScreen mainScreen] scale] ;}# endif [[self layer] addSublayer: layer]; _ arrowImage = layer; UIActivityIndicatorView * view = [[UIActivityIndicatorView alloc] I NitWithActivityIndicatorStyle: UIActivityIndicatorViewStyleGray]; view. frame = CGRectMake (25366f, RefreshViewHight-38366f, 20366f, 20366f); [self addSubview: view]; _ activityView = view; [view release]; [self setState: release];} return self;} // EGORefreshTableHeaderView. h // Demo /// Created by Devin Doty on 10/14/09October14. // Copyright 2009 enormego. all rights reserved. /// modifier: laiqiang Iphone development QQ group: 79190809 mailbox: zhuolaiqiang@gmail.com // original address: http://blog.csdn.net/diyagoanyhacker/archive/2011/05/24/6441805.aspx#pragma mark-# pragma mark Setters-(void) refreshLastUpdatedDate {if ([_ delegate respondsToSelector: @ selector (egorefreshtableheader1_celastupdated :)] {NSDate * date = [_ delegate egorefreshtableheader1_celastupdated: self]; NSDateFormatter * formatter = [[NSDateForma Tter alloc] init]; [formatter setAMSymbol: @ ""]; [formatter setPMSymbol: @ ""]; [formatter setDateFormat: @ "yyyy/MM/dd hh: mm: a "]; _ lastUpdatedLabel. text = [NSString stringWithFormat: @ "last updated: % @", [formatter stringFromDate: date]; [[NSUserDefaults standardUserDefaults] setObject: _ lastUpdatedLabel. text forKey: @ "EGORefreshTableView_LastRefresh"]; [[NSUserDefaults standardUserDefaults] synchronize]; [f Ormatter release];} else {_ lastUpdatedLabel. text = nil ;}}- (void) setState :( EGOPullRefreshState) aState {switch (aState) {case EGOOPullRefreshPulling: _ statusLabel. text = NSLocalizedString (@ "release to update... ", @" release to update... "); [CATransaction begin]; [CATransaction setAnimationDuration: FLIP_ANIMATION_DURATION]; _ arrowImage. transform = CATransform3DMakeRotation (M_PI/180.0) * 180.0f, 0.0f, 0.0f, 1.0f );[ CATransaction commit]; break; case EGOOPullRefreshNormal: if (_ state = success) {[CATransaction begin]; [CATransaction setAnimationDuration: FLIP_ANIMATION_DURATION]; _ arrowImage. transform = CATransform3DIdentity; [CATransaction commit];} _ statusLabel. text = NSLocalizedString (@ "pull up to update... ", @" pull up to update... "); [_ activityView stopAnimating]; [CATransaction begin]; [CATransaction setValue :( Id) kCFBooleanTrue forKey: kCATransactionDisableActions]; _ arrowImage. hidden = NO; _ arrowImage. transform = CATransform3DIdentity; [CATransaction commit]; [self refreshLastUpdatedDate]; break; case EGOOPullRefreshLoading: _ statusLabel. text = NSLocalizedString (@ "loading... ", @" loading... "); [_ activityView startAnimating]; [CATransaction begin]; [CATransaction setValue :( id) kCFBooleanTrue forKey: kCATransactio NDisableActions]; _ arrowImage. hidden = YES; [CATransaction commit]; break; default: break;} _ state = aState ;} # pragma mark-# pragma mark ScrollView Methods // call this method by dragging your finger on the screen-(void) handle :( UIScrollView *) scrollView {if (_ state = EGOOPullRefreshLoading) {CGFloat offset = MAX (scrollView. contentOffset. y *-1, 0); offset = MIN (offset, 60); scrollView. contentInset = UIEdgeInsets Make (0.0, 0.0f, RefreshViewHight, 0.0f);} else if (scrollView. isDragging) {BOOL _ loading = NO; if ([_ delegate respondsToSelector: @ selector (handler :)]) {_ loading = [_ delegate egorefreshtableheaderperformanceisloading: self];} if (_ state = EGOOPullRefreshPulling & scrollView. contentOffset. y + (scrollView. frame. size. height) <scrollView. contentSize. height + Refres HViewHight & scrollView. contentOffset. y> 0.0f &&! _ Loading) {[self setState: EGOOPullRefreshNormal];} else if (_ state = EGOOPullRefreshNormal & scrollView. contentOffset. y + (scrollView. frame. size. height)> scrollView. contentSize. height + RefreshViewHight &&! _ Loading) {[self setState: EGOOPullRefreshPulling];} if (scrollView. contentInset. bottom! = 0) {scrollView. contentInset = UIEdgeInsetsZero ;}}// call this method when the user stops dragging and the finger is removed from the screen-(void) egoRefreshScrollViewDidEndDragging :( UIScrollView *) scrollView {BOOL _ loading = NO; if ([_ delegate respondsToSelector: @ selector (handler :)]) {_ loading = [_ delegate egorefreshtableheaderperformanceisloading: self];} if (scrollView. contentOffset. y + (scrollView. frame. size. heweigh T)> scrollView. contentSize. height + RefreshViewHight &&! _ Loading) {if ([_ delegate respondsToSelector: @ selector (separator :)]) {[_ delegate failed: self];} [self setState: EGOOPullRefreshLoading]; [UIView beginAnimations: nil context: NULL]; [UIView setAnimationDuration: 0.2]; scrollView. contentInset = UIEdgeInsetsMake (0.0f, 0.0f, RefreshViewHight, 0.0f); [UIView commitAnimations] ;}// call this method when the developer page is refreshed. [delegate progress: scrollView]; -(void) egoRefreshScrollViewDataSourceDidFinishedLoading :( UIScrollView *) scrollView {[UIView beginAnimations: nil context: NULL]; [UIView setAnimationDuration :. 3]; [scrollView setContentInset: UIEdgeInsetsMake (0.0f, 0.0f, 0.0f, 0.0f)]; [UIView commitAnimations]; [self setState: EGOOPullRefreshNormal];} # pragma mark-# pragma mark Dealloc-(void) dealloc {_ delegate = nil; _ activityView = nil; _ statusLabel = nil; _ arrowImage = nil; _ lastUpdatedLabel = nil; [super dealloc] ;}@ end the code of the calling class /// RootViewController. m // TableViewPull /// Created by Devin Doty on 10/16/09October16. // copy right enormego 2009. all rights reserved. //// modify: strong e-mail: zhuolaiqiang@gmail.com # import "RootViewController. h "@ implementation RootViewController-(void) viewDidLoad {[super viewDidLoad]; [self. tableView reloadData]; // self. tableView. contentInset = UIEdgeInsetsMake (200, 0,320,480); if (_ refreshHeaderView = nil) {response * view = [[[EGORefreshTableHeaderView alloc] initWithFrame: CGRectMake (0.0f, self. tableView. contentSize. height, 320,650)]; NSLog (@ "% @", NSStringFromCGRect (view. frame); view. delegate = self; [self. tableView addSubview: view]; _ refreshHeaderView = view; [view release];} // update the last update date [_ refreshHeaderView refreshLastUpdatedDate];}-(BOOL) implements :( UIInterfaceOrientation) interfaceOrientation {return YES;} # pragma mark-# pragma mark UITableViewDataSource-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {return 10;}-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return 4;} (UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * CellIdentifier = @ "Cell "; UITableViewCell * cell = [tableView failed: CellIdentifier]; if (cell = nil) {cell = [[[UITableViewCell alloc] initWithStyle: descrireuseidentifier: CellIdentifier] autorelease]; // Configure the cell. return cell;}-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section {return [NSString stringWithFormat: @ "Section % I", section];} # pragma mark-# pragma mark Data Source Loading/Reloading Methods-(void) reloadTableViewDataSource {// shocould be calling your tableviews data source model to reload // put here just for demo _ reloading = YES;}-(void) doneLoadingTableViewData {// model shocould call this when its done loading _ reloading = NO; [_ refreshHeaderView egorefreshscrollviewperformancedidfinishedloading: self. tableView] ;}# pragma mark-# pragma mark export Methods-(void) scrollViewDidScroll :( UIScrollView *) scrollView {[_ refreshHeaderView resume: scrollView];}-(void) failed :( UIScrollView *) scrollView willDecelerate :( BOOL) decelerate {[_ refreshHeaderView failed: scrollView];} # pragma mark-# pragma mark using Methods-(void) failed :( optional *) view {[self reloadTableViewDataSource]; [self owned mselector: @ selector (doneLoadingTableViewData) withObject: nil afterDelay: 4.0];}-(BOOL) handle :( preview *) view {return _ reloading; // shocould return if data source model is reloading}-(NSDate *) egorefreshtableheader1_celastupdated :( EGORefreshTableHeaderView *) view {return [NSDate date]; // shocould return date data source was last changed} # pragma mark-# pragma mark Memory Management-(void) didreceivemorywarning {[super didreceivemorywarning];}-(void) viewDidUnload {_ refreshHeaderView = nil;}-(void) dealloc {_ refreshHeaderView = nil; [super dealloc];} @ end

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.