当通过滚动视图快速滚动时,我不确定内容是否加载得不够快或绘图被跳过,因为我通过检查 scrollViewDidEndDecelerating 生成新的子视图:(有更好的方法吗?)不需要快速滚动!- 我只是想解决它引起的问题。
更长的解释:我有一个滚动视图,内容很大并且启用了分页。对于给定的页面,添加三个子视图,其框架原点为 -1、0、+1 倍框架大小(因此中间框架可见,其他是下一页和上一页)。Frame -1 和 +1 是 ImageViews,Frame 0 是另一个 UIScrollView,有自己的 ImageView 子视图。它们不是子类。
出于性能原因,我的方法是检查滚动是否向前或向后移动,并且只加载新图像,因为另外两个已经加载,我存储它们,例如“UIImage *tempImg1 = previousPageImageView.image”并从那里获取它们一旦添加了子视图。
现在快速滚动会导致新图像不会出现,但更糟糕的是,程序会混淆它应该在哪个索引处并崩溃。我用小图片尝试过,但同样的问题。
不一定允许用户快速滚动,一旦开始页面移动直到加载完成,忽略进一步的滑动手势可能就足够了。
非常感谢您的想法。
页面加载:
-(void)loadSVforPage:(int)page
{
int nextSVpage = page +1;
int previousSVpage = page -1;
int numberOfAssets = assets.count;
ALAsset *displayAsset;
ALAssetRepresentation *assetRep;
CGFloat SVwidth = scrollLarge.frame.size.width;
CGFloat SVheight = scrollLarge.frame.size.height;
UIImage *tempImg1;
UIImage *tempImg2;
UIImage *tempImg3;
UIImage *displayed;
if (page != currentpage)
{
// if moves are +1 or -1 use temp images, else load all
if (page == currentpage +1) // moving forward
{
//NSLog(@"page+");
tempImg1 = nextSVImg; // the former "next", which now becomes current
tempImg3 = currentSVImg; // the former "current", which now becomes previous
if ((page+1 >=0) && (page+1 <= assets.count-1))
{
displayAsset = [assets objectAtIndex:page+1]; // the "new next" image
assetRep = [displayAsset defaultRepresentation];
tempImg2 = [ImageProcessing fullSizeImageForAssetRepresentation:assetRep];
}
}
else if (page == currentpage -1) // moving backwards
....
....
....
// LOAD PAGE+1
if (nextSVpage <= numberOfAssets-1)
{
displayed = tempImg2;
nextSVImg = displayed; // store the image for future scrolls
CGRect frame = CGRectMake(nextSVpage * SVwidth, 0, SVwidth, SVheight);
//old code adding UIImageViews
//------------------------------
//nextScrollImageView = [[UIImageView alloc]initWithFrame:frame];
//[completeScrollableImageView addSubview:nextScrollImageView];
//nextScrollImageView.image = displayed;
// new code with subclassed UIView and image in drawRect
//-------------------------------
scrollUIViewNext = [[scrollviewImage alloc]initWithFrame:frame andImage:displayed];
[completeScrollableImageView addSubview:scrollUIViewNext];
....
....
....
触发负载:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
if (scrollView.tag == 0) // the base SV
{
int pageNumber = floor(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5);//+1;
if (pageNumber != currentpage)
{
//NSLog(@"moved from current %i to %i",currentpage,pageNumber);
[self loadSVforPage:pageNumber];
}
}
}