论坛类浏览帖翻页tableView
很多论坛类的app都使用了此效果来达到浏览效果 https://github.com/lvjiaqijiaqi/JQPagerScan
2016-07-29 22_32_47.gif 2016-07-29 22_34_59.gif说一说里面的东西
1.因为只用一个tableView来来做的,每次需要跳转页码的时候,做截屏, 产生一个View来做过度动画。#####
-(UIImageView *)createMaskView{
UIImageView *maskView = [[UIImageView alloc] initWithFrame:CGRectMake([_PageTableView frameX], [_PageTableView frameY], [_PageTableView frameW],[_PageTableView frameH])];
maskView.image = [self convertViewToImage:_PageTableView.superview];
return maskView;
}
-(UIImage*)convertViewToImage:(UIView *)v{
CGSize s = CGSizeMake(v.frame.size.width, v.frame.size.height);
UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);
CGContextRef context = UIGraphicsGetCurrentContext();
[v.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRef ref = CGImageCreateWithImageInRect(img.CGImage, CGRectMake([_PageTableView frameX]*[UIScreen mainScreen].scale, [_PageTableView frameY]*[UIScreen mainScreen].scale, [_PageTableView frameW]*[UIScreen mainScreen].scale, [_PageTableView frameH]*[UIScreen mainScreen].scale));
UIImage *image = [UIImage imageWithCGImage:ref];
CGImageRelease(ref);
return image;
}
convertViewToImage:就是将view转化成image的method
2.阻止tableView的惯性滑动。#####
自己试一下,当scrollView滑动的时候会有惯性,当手抬起的时候,scrollView将做一个减速滑动, 这个惯性滑动会传递下去,
比如当你处于减速滑动的时候,刷新了的tableView,但是惯性滑动会继续进行下去,所以当我们通过手势翻到下一页的时候,应该阻止惯性滑动。
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
NSInteger p = [self validNewPage:scrollView];
if (p != -1 ) {
dispatch_async(dispatch_get_main_queue(), ^{
[scrollView setContentOffset:scrollView.contentOffset animated:NO];
[scrollView setNeedsLayout];
[scrollView layoutIfNeeded];
[self setCurrentPage:p];
});
}
}
:: validNewPage:scrollView是一个验证是否能翻页的方法.
在scrollViewDidEndDragging方法中 [scrollView setContentOffset:scrollView.contentOffset animated:NO]消除惯性滑动,同时强制刷新,之后再来处理翻页的行为.
3.autoLayout带来的问题。#####
采用了autolayout驱动的tableview并不会一开始就设置正确的tableview.contentSize,在滑到最后一个cell,遍历了整个cell的约束的时候才会达到正确contentSize , 所以造成的影响就是tableview下方的提示label的orgin不能在tableview刷新的时候确定。要根据tableview.contentSize变化来确定。
-(void)adjustTheTailY:(CGFloat)h{
if (h == 0) {self.tailLabelOfTable.hidden = YES;return;}
self.tailLabelOfTable.hidden = NO;
[self.tailLabelOfTable setCenter:CGPointMake(self.tailLabelOfTable.center.x, h+50)];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
if ([keyPath isEqual:@"contentSize"]){
[self adjustTheTailY:[change[@"new"] CGSizeValue].height];
}else if ([keyPath isEqual:@"contentOffset"]){
}
}
使用也是很简单的
self.pagerScanView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-50)];
self.pagerScanView.dataSource = self;
[self.pagerScanView registerNib:[UINib nibWithNibName:@"PageTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
self.pagerScanView.rowHeight = UITableViewAutomaticDimension;
[self.view addSubview:self.pagerScanView];
self.data = [[NSMutableDictionary alloc] init];
__weak typeof(self)weakSelf = self;
[self.pagerScanView statPageScanBlock:^(UITableView *tabelView , void (^callBack)(NSDictionary *headMsg) ) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{
NSData *data = tabelView.tag%2 ? [NSData dataWithContentsOfURL:[NSBundle.mainBundle URLForResource:@"data" withExtension:@"txt"]] : [NSData dataWithContentsOfURL:[NSBundle.mainBundle URLForResource:@"data1" withExtension:@"txt"]] ;
NSDictionary *a = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSArray *floors = [a objectForKey:@"postsFloor"];
NSDictionary *msg = [a objectForKey:@"postMsg"];
NSMutableArray *d = [NSMutableArray arrayWithCapacity:10];
for (int i = 0; i < 10; i++) {
NSDictionary *floor = [floors objectAtIndex:i];
Floor *f = [[Floor alloc] init];
f.floorNum = [NSString stringWithFormat:@"%@",[floor objectForKey:@"floorNum"]];
f.username = [floor objectForKey:@"name"];
f.body = [floor objectForKey:@"content"];
f.status = @"";
f.time = [floor objectForKey:@"time"];
[d addObject:f];
}
__weak id weakTableView = tabelView;
[weakSelf.data setObject:d forKey:[NSNumber numberWithInteger:tabelView.tag]];
dispatch_async(dispatch_get_main_queue(), ^{
[weakTableView reloadData];
callBack(msg);
});
});
}];
[self.pagerScanView setReplyBlock:^(NSString *uid) {
NSLog(@"setReplyBlock");
}];
[self.pagerScanView setCommentBlock:^(NSString *uid) {
NSLog(@"setCommentBlock");
}];
[self.view addSubview:[self.pagerScanView controllView]];
statPageScanBlock 方法需要传进去一个block,在这个block里面获得数据,最后刷新tableView , 数据的保存用了一个可变的字典,key是页码数。
callBack(msg)函数是一个回调函数,定义了获取数据后需要执行的动作,参数msg是帖子的信息。
上面是整个demo的架构 , 通过类扩展将JQPagerControll作为tableView的属性,tabelview和menuView的事件发送给JQPagerControll去处理(是否跳转,跳转到哪,跳转)。