EGOTableViewPullRefresh 是fork EGOTableViewPullRefresh开源类库进行的改进,添加了上提加载更多效果。同时也可以通过一个按钮的触发刷新事件,但是刷新的时候不能跳到top,为了动态展示,再刷新的时候按钮旋转,然后跳转回到顶部!如下如图
关于EGOTableViewPullRefresh可以参照http://blog.csdn.net/duxinfeng2010/article/details/9007311,翻译过的用法,在这个Demo基础上进行修改,点击Demo下载;
1、给工程添加一个导航栏,在application: didFinishLaunchingWithOptions:方法中
1
2
3
4
5
6
7
8
9
10
|
- (
BOOL
)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return
YES;
}
|
2、在ViewDidLoad方法中,修改背景图片,添加刷新按钮
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
- (
void
)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@
"navbar.png"
]];
self.pullTableView.pullArrowImage = [UIImage imageNamed:@
"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 imageNamed:@
"button_refresh"
] forState:UIControlStateNormal];
[refreshBtn addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithCustomView:refreshBtn];
self.navigationItem.leftBarButtonItem = refreshItem;
}
|
|
3、添加刷新按钮事件,和按钮旋转方法
1
2
3
4
5
6
7
8
9
10
11
12
|
//按钮旋转
- (
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"
];
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
-(
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
本文转自新风作浪 51CTO博客,原文链接:http://blog.51cto.com/duxinfeng/1284937,如需转载请自行联系原作者