scrollViewDidScroll委托方法和更新UI时出现问题
我有一个滚动视图,并且正在通过页面控件实现lazyload.我已经通过apple提取了pagecontrol示例程序.
I have a scroll view and i am implementing lazyload with page control. I have taken the pagecontrol sample program by apple.
我在每个页面中加载8个缩略图,并且缩略图本身是从网络中获取并在UI上更新的.图像视图已经存在于UI中.在我的viewDidScroll方法中,我计算页码,然后如下更新其上方和下方的其他页面:
I load 8 thumbnails in every page and the thumbnails themselves are fetched from the network and updated on the UI. The image views are already present in the UI. In my viewDidScroll method i calculate the page number and then update the other pages above and below it as follows:
BOOL isScrollingDown = verticalScrollView.contentOffset.y > _previousContentOffsetY;
_previousContentOffsetY = verticalScrollView.contentOffset.y;
CGFloat pageHeight = verticalScrollView.frame.size.height;
int scrollingToPageNum = isScrollingDown ? (ceil((verticalScrollView.contentOffset.y - pageHeight) / pageHeight) + 1) : (floor((verticalScrollView.contentOffset.y - pageHeight) / pageHeight) + 1);
int page = floor((verticalScrollView.contentOffset.y - pageHeight / 2) / pageHeight) + 1;
[self loadPage:(page-1)];
[self loadPage:(page)];
[self loadPage:(page+1)];
/* Unloading the pages not seen in the view is done here*/
if (!(isScrollingDown) && scrollingToPageNum >1) {
[self unloadPages:page-2];
}else {
[self unloadPages:page+2];
}
UI太慢,用户体验非常差.我想知道如何使UI更具响应性.
The UI is too slow and the user experience is very bad. I want to know how can i make the UI more responsive.
其他一些问题是: 1)我有一个下载图像的类,调用类实现了下载器类的委托.下载图像后,将调用委托方法来更新UI.这会减慢UI的响应速度吗?如何避免这种情况? 2)在不使UI变得无响应的情况下,实现图像下载和更新ui的最佳方法是什么?
Some other questions are: 1) I have a class which downloads the images and the calling class implements the delegate for the downloader class. Once the image is downloaded the delegate method is called to update the UI. Does this slow down the responsiveness of the UI? How can this be avoided? 2) What is a optimal way to implement downloading of images and updating the ui without having the UI become unresponsive?
我对如何最好地改进和实施此解决方案的想法持开放态度. 我不是在显式创建线程,而是在异步模式下使用NSURLConnection,并且在检索数据时,将调用委托方法来更新UI.
I am open to ideas on how to best improve and implement this solution. I am not creating a thread explicitly but i am using NSURLConnection in async mode, and when data is retrieved the delegate method is called to update the UI.
我认为您可以从Apple示例LazyTableImages中使用的某些技术中受益匪浅.虽然以表格视图为例,但这些概念可以轻松应用于滚动视图.
I think you could greatly benefit from some of the techniques used in the Apple sample LazyTableImages. While it uses a table view as an example, the concepts can easily be applied to a scroll view.