iOS:UIScrollView分页无法正常工作 [英] iOS : UIScrollView paging not working properly
问题描述
我正在将AsyncImageView
添加到UIScrollView
以从Web加载图像,它的工作正常,我添加了8张图像.问题是paging
,我已将pagingEnable
启用为YES
,但是并没有达到我想要的效果,我正在尝试将其设置为contentOffset
,并使用不同的x
,但不会有任何效果.我希望在滚动或选择next
或previous
选项时,每个图像都应出现在UIScrollView
的center
中.
I'm adding AsyncImageView
into UIScrollView
to load images from web, its working fine, there are 8 images i've added. Problem is of paging
I've enabled pagingEnable
to YES
but it won't come as per I want, I'm trying to setting it contentOffset
with different x
but it won't any effects. I want that each image should come in center
of UIScrollView
when I scroll or choose next
or previous
option.
这是我将图像添加到UIScrollView
arrImgUrls是一个NSArray
arrImgUrls is an NSArray
_currentImage,_imageCount是整数
_currentImage, _imageCount are integers
-(void)setImagesToScrollView
{
[btnPrev setEnabled:NO];
_currentImage = 0;
_imageCount = [arrImgUrls count];
int x=20;
int y=0;
int w=213;
int h=160;
for (int i=0; i<[arrImgUrls count]; i++)
{
AsyncImageView *asyncImageView = [[AsyncImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];
[asyncImageView setDelegate:self];
[asyncImageView.layer setMasksToBounds:YES];
NSString *urlImage = [arrImgUrls objectAtIndex:i];
NSURL *url = [NSURL URLWithString:urlImage];
[asyncImageView loadImageFromURL:url];
[asyncImageView release];
[scrollImages addSubview:asyncImageView];
x=(x+w)+10;
}
scrollImages.contentSize=CGSizeMake(x, scrollImages.frame.size.height);
}
这是我用来在UIScrollView
中显示previous
或next
图像的代码.
This is code I used to show previous
or next
images in UIScrollView
.
-(IBAction)prevImage:(id)sender
{
_currentImage--;
[btnNext setEnabled:YES];
if (_currentImage<=0)
{
_currentImage = 0;
[btnPrev setEnabled:NO];
}
[scrollImages setContentOffset:CGPointMake((_currentImage*imageWidth)+10, 0) animated:NO];
}
-(IBAction)nextImage:(id)sender
{
_currentImage++;
[btnPrev setEnabled:YES];
if (_imageCount-1 == _currentImage)
{
[btnNext setEnabled:NO];
}
[scrollImages setContentOffset:CGPointMake(_currentImage*imageWidth, 0) animated:NO];
}
所有加载并添加到UIScrollView
的图像的唯一问题是,当用户scrolls
或执行previous
或next
时,在center
中显示该图像.
All images loaded and added to UIScrollView
only problem is to show it in center
when user scrolls
or do previous
or next
.
请指出我做错了什么地方
Please point me where I'm doing wrong?
谢谢!
推荐答案
首先要确保_currentImage
是否正确维护?
First of all make sure that _currentImage
is maintain properly or not?
现在尝试关注下一张图片
Now try following for next image
CGRect frame;
frame.origin.x = (imageWidth * _currentImage) +10;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES]; // NO if you don't want animation
代替此行
[scrollImages setContentOffset:CGPointMake(_currentImage*imageWidth+10, 0) animated:NO];
&对于上一张图片
& for previous image
CGRect frame;
frame.origin.x = (imageWidth * _currentImage) - 10;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES]; // NO if you don't want animation
还有一个很好的例子,就像您想要的
and here one nice example just like you want
这篇关于iOS:UIScrollView分页无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!