我试图重构我的代码以获得更好的内存管理和性能。我有一个包含12个图像的数组,但我想根据需要加载它们,而不是同时加载它们。也许是当前图像-1
,当前图像和当前图像+1
。我在SO上查看了一些答案,但发现它们不清楚。如果我发布我的代码以供参考,我觉得会更清楚。
//scroll view set up
- (void)scrollViewSetUp
{
self.scrollview.delegate = self;
for (int i = 0; i < _images.count; i++)
{
CGRect frame;
frame.origin.x = self.scrollview.frame.size.width * i;
frame.size = self.scrollview.frame.size;
self.scrollview.pagingEnabled = YES;
UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
subview.image = [UIImage imageNamed:[_images objectAtIndex:i]];
[self.scrollview addSubview:subview];
}
self.scrollview.contentSize = CGSizeMake(self.scrollview.frame.size.width * _images.count, self.scrollview.frame.size.height);
self.automaticallyAdjustsScrollViewInsets = NO;
//page control ie .... at bottom of
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100.0,0.0,100.0,40.0)];
[self.pageControl setNumberOfPages:_images.count];
[self.pageControl setCurrentPage:0];
self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
self.pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
[self.pageControl setBackgroundColor:[UIColor clearColor]];
[self.viewForPageControl addSubview:self.pageControl];
[self.viewForPageControl setBackgroundColor:[UIColor clearColor]];
}
#pragma mark - UIScrollView Delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
int newOffset = scrollView.contentOffset.x;
int newPage = (int)(newOffset/(scrollView.frame.size.width));
[_pageControl setCurrentPage:newPage];
}
非常感谢任何建议或指示。
答案 0 :(得分:0)
您可以尝试重新使用UIImageView
,因为它们会在屏幕外显示...但您只是想重做UITableview
已经执行的操作。带有自定义UITableView
的{{1}}会在新细胞可见时将您通知,并将屏幕外的细胞出列。
UITableViewCell
答案 1 :(得分:0)
不确定这是否是最佳方式,但这是一种选择。
设置scrollView时,可以在UIImageView上设置tag属性,使其与循环中的索引相对应。
然后在你的scrollViewDidEndDecelerating方法中:
for (int i = 0; i < _salonImages.count; i++)
{
if(i == (newPage-1))
{
UIImageView *imgView = (UIImageView*)[self.scrollView viewWithTag:newPage-1];
imgView.image = [_images objectAtIndex:newPage-1];
}
else if(i == newPage)
{
UIImageView *imgView = (UIImageView*)[self.scrollView viewWithTag:newPage];
imgView.image = [_images objectAtIndex:newPage];
}
else if(i == (newPage + 1 ))
{
UIImageView *imgView = (UIImageView*)[self.scrollView viewWithTag:newPage+1];
imgView.image = [_images objectAtIndex:newPage+1];
}
else
{
UIImageView *imgView = (UIImageView*)[self.scrollView viewWithTag:i];
imgView.image = nil;
}
}
答案 2 :(得分:0)
根据Gman的要求,我转发了我的评论。
本教程将为您提供帮助:
http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content