【请教】关于 仿新浪微博下拉刷新Listview!!
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了【请教】关于 仿新浪微博下拉刷新Listview!!相关的知识,希望对你有一定的参考价值。
在网上看到很多所谓的 仿新浪微博下拉刷新 Listview。。。 搜索引擎一搜一大把,但是细看其实都是PullToRefreshListView这个控件。 PullToRefreshListView这个控件,做android的肯定不陌生,是一个外国人写的控件(继承Listview) 一直拿来主义的我,却发现这个控件其实并不出色,下拉到可刷新位置时会卡顿,而且经常卡住。 但是,看到许多应用中的这一控件却十分流程(新浪微博,网易新闻,陌陌(这个尤为的好,下拉时箭头是随用户拉动而旋转的)。。。) 所以,不禁怀疑,他们并不是使用的这个网上流传的控件。 是不是使用的ScrollView呢?求真相
参考技术A 呵呵,抱歉啊,没细看,你这个问题就是shenallan所说的了,处理问题,onTouchEvent里没处理好 参考技术B 这个流不流畅关键看touch事件的处理吧。用什么控件应该差不大吧 参考技术C OnScrollListener和OnTouchListener一块用就哦了IOS 使用SDWebImage实现仿新浪微博照片浏览器
使用第三方库SDWebImage实现仿新浪微博照片浏览器,可以下载图片缓存,点击之后滚动查看相片,具体效果如下:
代码如下:
WeiboImageView.h:
#import <UIKit/UIKit.h> @interface WeiboImageView : UIImageView @property (nonatomic, assign) CGRect originRect; - (instancetype)initWithFrame:(CGRect)frame; @end
WeiboImageView.m
#import "WeiboImageView.h" @implementation WeiboImageView @synthesize originRect; - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { originRect = frame; self.clipsToBounds = YES; self.userInteractionEnabled = YES; self.contentMode = UIViewContentModeScaleAspectFill; } return self; } - (void)dealloc { } @end
WeiboImageBrowser.h
#import <UIKit/UIKit.h> @interface WeiboImageBrowser : UIView<UIScrollViewDelegate> //@property (nonatomic, copy) NSArray *imageViewArray; @property (nonatomic, assign) int currentSelectedIamge; @property (nonatomic, strong) UIScrollView *backgroundScrollView; @property (nonatomic, strong) UILabel *pageIndexLabel; @property (nonatomic,strong) NSMutableArray *originRects; @property (nonatomic, copy) NSArray *bigImageArray; - (instancetype)initWithFrame : (CGRect)rect; - (void)showWeiboImages; @end
WeiboImageBrowser.m
#import "WeiboImageBrowser.h" #import "WeiboDefine.h" #import "WeiboImageView.h" #import "SDImageCache.h" //缓存相关 #import "SDWebImageCompat.h" //组件相关 #import "SDWebImageDecoder.h" //解码相关 //图片下载以及下载管理器 #import "SDWebImageDownloader.h" #import "SDWebImageManager.h" #import "SDWebImageDownloaderOperation.h" @implementation WeiboImageBrowser //@synthesize imageViewArray; @synthesize currentSelectedIamge; @synthesize bigImageArray; - (instancetype)initWithFrame : (CGRect)rect { self = [super initWithFrame:rect]; if (self) { [self addSubview:self.pageIndexLabel]; self.backgroundColor = [UIColor blackColor]; [self initBackgroundScrollView]; } return self; } - (UIScrollView *)backgroundScrollView { if (!_backgroundScrollView) { _backgroundScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT)]; _backgroundScrollView.pagingEnabled = YES; } return _backgroundScrollView; } - (NSMutableArray *)originRects { if (!_originRects) { _originRects = [NSMutableArray array]; } return _originRects; } - (UILabel *)pageIndexLabel { if (!_pageIndexLabel) { _pageIndexLabel = [[UILabel alloc]initWithFrame:CGRectMake((UISCREEN_WIDTH - 100) / 2, UISCREEN_HEIGHT - 50, 100, 25)]; _pageIndexLabel.textAlignment = NSTextAlignmentCenter; _pageIndexLabel.textColor = [UIColor whiteColor]; _pageIndexLabel.font = [UIFont fontWithName:@"Arial" size:20]; } return _pageIndexLabel; } - (void)initBackgroundScrollView { self.backgroundScrollView.contentSize = CGSizeMake([self.bigImageArray count] * UISCREEN_WIDTH, UISCREEN_HEIGHT); self.backgroundScrollView.delegate = self; self.backgroundScrollView.showsHorizontalScrollIndicator = NO; [self addSubview:self.backgroundScrollView]; } - (void)initImageScrollViews { self.backgroundScrollView.contentSize = CGSizeMake([self.bigImageArray count] * UISCREEN_WIDTH, 0); self.backgroundScrollView.contentOffset = CGPointMake(currentSelectedIamge * UISCREEN_WIDTH, 0); for (int i = 0; i < [self.bigImageArray count]; i++) { [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:[self.bigImageArray objectAtIndex:i] ] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) { NSLog(@"progress!!!"); } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { UIScrollView *iamgeScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(UISCREEN_WIDTH * i, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT)]; WeiboImageView *weibImageView = [[WeiboImageView alloc]init]; weibImageView.image = image; CGFloat ratio = (double)weibImageView.image.size.height / (double)weibImageView.image.size.width; weibImageView.bounds = CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_WIDTH * ratio); weibImageView.center = CGPointMake(UISCREEN_WIDTH / 2, UISCREEN_HEIGHT / 2); UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTap:)]; [weibImageView addGestureRecognizer:tap]; [iamgeScrollView addSubview:weibImageView]; [self.backgroundScrollView addSubview:iamgeScrollView]; }]; } } - (void)showWeiboImages { [self initImageScrollViews]; [[UIApplication sharedApplication].keyWindow addSubview:self]; } -(void)imageTap:(UITapGestureRecognizer *)tap { [self removeFromSuperview]; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { int nCurrentPage = scrollView.contentOffset.x / UISCREEN_WIDTH; currentSelectedIamge = nCurrentPage; NSString *stringPage = [NSString stringWithFormat:@"%d/%d",nCurrentPage + 1, [self.bigImageArray count]]; self.pageIndexLabel.text = stringPage; [self bringSubviewToFront:self.pageIndexLabel]; } @end
使用方法:
ViewController.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (nonatomic, strong)NSArray *imageArray; @end
ViewController.m
#import "ViewController.h" #import "SDImageCache.h" //缓存相关 #import "SDWebImageCompat.h" //组件相关 #import "SDWebImageDecoder.h" //解码相关 //图片下载以及下载管理器 #import "SDWebImageDownloader.h" #import "SDWebImageManager.h" #import "SDWebImageDownloaderOperation.h" #import "WeiboDefine.h" #import "WeiboImageView.h" #import "WeiboImageBrowser.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; float imageWidth = (UISCREEN_WIDTH - 50) / 3.0; _imageArray = [NSArray arrayWithObjects: @"http://g.hiphotos.baidu.com/image/pic/item/c2cec3fdfc03924578c6cfe18394a4c27c1e25e8.jpg", @"http://image.tianjimedia.com/uploadImages/2015/072/23/4X4L0E9BNEZ6.jpg", @"http://img15.3lian.com/2015/c1/83/d/31.jpg", @"http://img15.3lian.com/2015/c1/83/d/31.jpg", @"http://img15.3lian.com/2015/c1/83/d/31.jpg", @"http://img15.3lian.com/2015/c1/83/d/31.jpg", @"http://img15.3lian.com/2015/c1/83/d/31.jpg", @"http://img15.3lian.com/2015/c1/83/d/31.jpg", @"http://img15.3lian.com/2015/c1/83/d/31.jpg",nil ] ; int n = _imageArray.count; for (int i = 0; i < n; i++) { [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:[_imageArray objectAtIndex:i] ] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) { NSLog(@"progress!!!"); } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { CGRect rect = CGRectMake(15 + (i % 3) * (imageWidth + 10) , 20 + 90 + (i / 3) * (imageWidth + 10), imageWidth, imageWidth); UIImageView *imageView = [[UIImageView alloc]initWithFrame:rect]; imageView.image = image; imageView.frame = rect; imageView.clipsToBounds = YES; imageView.contentMode = UIViewContentModeScaleAspectFill; imageView.tag = i; imageView.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTap:)]; [imageView addGestureRecognizer:tap]; [self.view addSubview:imageView]; }]; } // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)imageTap:(UITapGestureRecognizer *)tap { WeiboImageBrowser *imageBrowser = [[WeiboImageBrowser alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT)]; imageBrowser.currentSelectedIamge = tap.view.tag; imageBrowser.bigImageArray = /*weiboInformation.pic_urls*/_imageArray; [imageBrowser showWeiboImages]; } @end
源代码下载链接:http://download.csdn.net/detail/lzm2625347497/9615067
以上是关于【请教】关于 仿新浪微博下拉刷新Listview!!的主要内容,如果未能解决你的问题,请参考以下文章
Listview嵌套Viewpager实现仿淘宝搜狐广告主页,并实现listview的下拉刷新
新浪微博客户端(18)-集成下拉刷新控件UIRefreshControl