EGOTableViewPullRefresh動態刷新按鈕

EGOTableViewPullRefresh 是fork EGOTableViewPullRefresh開源類庫進行的改進,添加了上提加載更多效果。同時也可以通過一個按鈕的觸發刷新事件,但是刷新的時候不能跳到top,爲了動態展示,再刷新的時候按鈕旋轉,然後跳轉回到頂部!如下如圖




關於EGOTableViewPullRefresh可以參照http://blog.csdn.net/duxinfeng2010/article/details/9007311,翻譯過的用法,在這個Demo基礎上進行修改,點擊Demo下載

1、給工程添加一個導航欄,在application: didFinishLaunchingWithOptions:方法中



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
//    [[UINavigationBar appearance] setBackgroundImage:[UIImage p_w_picpathNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
               
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    return YES;
}



2、在ViewDidLoad方法中,修改背景圖片,添加刷新按鈕


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage p_w_picpathNamed:@"navbar.png"]];
         
    self.pullTableView.pullArrowImage = [UIImage p_w_picpathNamed:@"blackArrow"];
//    self.pullTableView.pullBackgroundColor = [UIColor yellowColor];
    self.pullTableView.pullTextColor = [UIColor blackColor];
         
    CGRect rect = CGRectMake(0, 0, 44, 44);
    UIButton *refreshBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    refreshBtn.frame = rect;
    [refreshBtn setBackgroundImage:[UIImage p_w_picpathNamed:@"button_refresh"] forState:UIControlStateNormal];
    [refreshBtn addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithCustomView:refreshBtn];
    self.navigationItem.leftBarButtonItem = refreshItem;
         
}



3、添加刷新按鈕事件,和按鈕旋轉方法



//按鈕旋轉
- (void)startAnimation:(UIButton *)button{
    CABasicAnimation *rotate =
    [CABasicAnimation animationWithKeyPath:@"transform.rotation"];   
    [rotate setByValue:[NSNumber numberWithFloat:M_PI*4]];
    rotate.duration = 3.0;
    rotate.timingFunction =
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [button.layer addAnimation:rotate
                        forKey:@"myRotationAnimation"];
           
}



-(void)refresh:(UIButton *)button
{
    [self startAnimation:button];
//    判斷一下table是否處於刷新狀態,如果沒有則執行本次刷新
    if (!self.pullTableView.pullTableIsRefreshing) {
        self.pullTableView.pullTableIsRefreshing = YES;
//        設置回到top時候table的位置
        [self.pullTableView setContentOffset:CGPointMake(0, -60) animated:YES];
        [self performSelector:@selector(refreshTable) withObject:nil afterDelay:3.0];
    }
                 
}



源碼下載地址:https://github.com/XFZLDXF/RefreshButtonDemo




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章