最近在写知乎日报,今天来总结一下最近遇到的问题。
- (void)test {
[[Manager sharedManager] NetworkQuestSuccess:^(NetWorkModel* _Nonnull mainViewNowModel) {
NSLog(@"请求成功");
self.netModel = mainViewNowModel;
// 回到主线程加载UI
dispatch_async(dispatch_get_main_queue(), ^{
// UI更新代码
[self addUI];
});
} error:^(NSError * _Nonnull error) {
NSLog(@"请求失败");
}];
}
因为更新UI需要请求到的数据,这里利用了通知传值
。
- (void)addUI {
// 通知传值传给View界面
_homeView = [[HomeView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:_homeView];
_dictionary = [NSDictionary dictionaryWithObject:self.netModel forKey:@"ansNetModel"];
[[NSNotificationCenter defaultCenter]postNotificationName:@"firstSender" object:nil userInfo:_dictionary];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
int page = scrollView.contentOffset.x / WIDTH;
NSLog(@"%d",page);
if (page == 0) {
_testPageControl.currentPage = page;
// 改变偏移量到最后一张图片
scrollView.contentOffset = CGPointMake(WIDTH * 5, 0);
_testPageControl.currentPage = 4;
} else if (page == 6) {
// 改变偏移量到第一张图片
scrollView.contentOffset = CGPointMake(WIDTH, 0);
_testPageControl.currentPage = 0;
} else {
_testPageControl.currentPage = page - 1;
}
// 重启计时器
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(runTimer) object:nil];
[self performSelector:@selector(runTimer) withObject:nil afterDelay:0.4];
}
- (void) changeImage {
if (self.currentImageIndex == 5) {
_testPageControl.currentPage = 0;
} else {
_testPageControl.currentPage = self.currentImageIndex;
}
CGFloat x = WIDTH * self.currentImageIndex + WIDTH;
[_topScollView setContentOffset:CGPointMake(x, 0) animated:YES];
if (self.currentImageIndex == 6) {
self.currentImageIndex = 0;
[_topScollView setContentOffset:CGPointMake(WIDTH, 0) animated:NO];
_testPageControl.currentPage = 0;
}
self.currentImageIndex++;
}
这里向左滑动时,计时器有点小崩,后续补充。。。
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
NSLog(@"did end drag");
if (self.tableView.contentOffset.y + HEIGHT> self.tableView.contentSize.height) {
NSLog(@"!");
[self.activityIndicator startAnimating];
[[Manager sharedManager] questBeforeData:^(NetWorkModel* _Nonnull mainViewNowModel) {
NSLog(@"请求成功");
for (int i = 0; i < 6; i++) {
[self->_myDateArray addObject:mainViewNowModel.stories[i]];
StoriesModel* story = mainViewNowModel.stories[i];
[self->_downUrlArray addObject:story.url];
}
NSLog(@"%@", self->_downUrlArray);
// 回到主线程加载UI
dispatch_async(dispatch_get_main_queue(), ^{
// UI更新代码
[self reloadTableView];
});
} error:^(NSError * _Nonnull error) {
NSLog(@"请求失败");
}];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _myDateArray.count + 1;
}
将请求到的数据加入数组,重新加载tableView。
就可以完成上拉加载了:
4. 上拉刷新的小菊花。
这里可以使用iOS自带的小菊花,只需要定义属性,设定位置,以及消失出现的时间即可。
// 设置小菊花
@property (nonatomic, strong) UIActivityIndicatorView * activityIndicator;
- (void)loadflower {
_activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleMedium)];
//设置小菊花的frame
_activityIndicator.frame= CGRectMake(0.4 * WIDTH, 0.9 * HEIGHT, 100, 100);
//设置小菊花颜色
_activityIndicator.color = [UIColor redColor];
//设置背景颜色
_activityIndicator.backgroundColor = [UIColor clearColor];
//刚进入这个界面会显示控件,并且停止旋转也会显示,只是没有在转动而已,没有设置或者设置为YES的时候,刚进入页面不会显示
_activityIndicator.hidesWhenStopped = YES;
[self addSubview:self.activityIndicator];
}
NSDate *lastDay = [NSDate dateWithTimeInterval:-24 * a * 60 * 60 sinceDate:date];//前一天
NSString* dateString = [lastDay description];
NSString* string = [[NSMutableString alloc] init];
string = [dateString substringWithRange:NSMakeRange(0, 10)];
NSCharacterSet *nonDigitCharacterSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
//提取数字后的字符串
string = [[string componentsSeparatedByCharactersInSet:nonDigitCharacterSet] componentsJoinedByString:@""];
// 加载上拉数据的时间
if (indexPath.row != 1 && (indexPath.row - 1) % 6 == 0) {
cell.dateLabel.hidden = NO;
cell.dateLabel.text = _dateArray[(indexPath.row - 1) / 6 - 1];
}
#import "UIImageView+WebCache.h"
然后可以根据图片地址获取图片
_topImage = [[UIImageView alloc] init];
_imageString = [_netModel.top_stories[k] valueForKey:@"image"];
NSLog(@"%@", [_netModel.top_stories[k] image]);
NSURL* url = [NSURL URLWithString:_imageString];
[_topImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"default"]]
- (void)receiveWebView:(NSNotification*)sender {
_webView = [[WKWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
NSURL *url = [NSURL URLWithString:sender.userInfo[@"webString"]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
[self loadScrollView];
}