iOS:如何在UIScrollView中进行正确的分页? [英] iOS : How to do proper paging in UIScrollView?
问题描述
我的UIScrollView
大小为(320,160).我要添加一些UIImageView
,其大小为(213,160).第一个UImageView
从54(x)开始,依此类推,我在每个UIImageView
之间添加了5.0的空格.我还启用了IB
&中的pagingEnable
在coding
中.我的问题是它的属性无法正常工作!当我滚动时,应该在每个page
中向我显示UIImageViews
,而不是向我显示查看屏幕截图我要输出类似这样的内容查看输出屏幕截图
I've a UIScrollView
of size (320,160). I'm adding some UIImageView
into it, which are of size (213,160). The first UImageView
starting from 54 (x) and so on, I've added a space of 5.0 in between each UIImageView
. I've also enabled pagingEnable
in IB
& in coding
. What my problem is its not properly working as per its property! When I scroll it should show me UIImageViews
in each single page
instead it showing me something like see screenshot I want output something like this see output screenshot
我在哪里做错了?我还具有<
(上一个)的功能. >
(下一个)在那里显示图像.昨天我问了一个问题,这是我接受的,但是我的要求几乎没有变化,并且它不会成为我的解决方案. 查看我的问题.
Where I'm doing wrong? I also having function of<
(previous) & >
(next) there to show images. I've asked one question yesterday which was I accepted however my requirement is little change and it won't become my solution. see my question.
我是否需要设置任何特殊属性,或者应该实现一些逻辑?我检查并尝试过的所有示例,但我发现我的要求有些特殊.指点我!谢谢.
Is there any special property that I've to set, or some logic I should implement? All examples I've checked and tried but I find that my requirement is some special. Point me! Thanks.
已编辑
- (void) setImages
{
CGFloat contentOffset = 0.0f;
for (int i=0; i<[arrImgUrls count]; i++)
{
CGRect imageViewFrame = CGRectMake(contentOffset, 0.0f, 213, scrollImages.frame.size.height);
AsyncImageView *asyncImageView = [[AsyncImageView alloc] initWithFrame:imageViewFrame];
[asyncImageView.layer setMasksToBounds:YES];
NSString *urlImage = [arrImgUrls objectAtIndex:i];
NSURL *url = [NSURL URLWithString:urlImage];
[asyncImageView loadImageFromURL:url];
[scrollImages addSubview:asyncImageView];
contentOffset += asyncImageView.frame.size.width+increment;
[asyncImageView release];
}
scrollImages.contentSize = CGSizeMake([arrImgUrls count] * scrollImages.frame.size.width, scrollImages.frame.size.height);
scrollImages.pagingEnabled = YES;
scrollImages.clipsToBounds = NO;
}
-(IBAction)prevImage:(id)sender
{
_currentImage--;
[btnNext setEnabled:YES];
if (_currentImage==0)
{
[btnPrev setEnabled:NO];
[scrollImages setContentOffset:CGPointMake((_currentImage*imageWidth), 0) animated:YES];
return;
}
NSLog(@"previous:mult %d inc %d current %d",_currentImage*imageWidth,increment*_currentImage,_currentImage);
int nextImage=_currentImage+2;
[scrollImages setContentOffset:CGPointMake((((_currentImage*imageWidth)-(increment*_currentImage)))+(nextImage*increment), 0) animated:YES];
}
-(IBAction)nextImage:(id)sender
{
_currentImage++;
NSLog(@"next:mult %d inc %d current %d",_currentImage*imageWidth,increment*_currentImage,_currentImage);
[scrollImages setContentOffset:CGPointMake((_currentImage*imageWidth)+(increment*_currentImage), 0) animated:YES];
[btnPrev setEnabled:YES];
if (_imageCount-1 == _currentImage)
{
[btnNext setEnabled:NO];
}
}
推荐答案
分页滚动视图始终是其帧大小的页面倍数.因此,在您的示例中,分页始终为+320.
Paging scroll views alway page multiples of their frame size. So in your example paging is always +320.
如果您的内容部分与滚动视图的框架匹配,则此行为很好.
This behavior is good if you have content portions matching the frame of the scroll view.
您需要做的是,使滚动视图的宽度为213,并将其clipsToBounds
属性设置为NO
.
之后,您的滚动视图将准确显示您想要的页面,并且您可以看到框架外部的内容.
另外,您还必须技巧一下,以使此左和右区域代表触摸到滚动视图.
What you have to do, is giving your scroll view a width of 213 and set its clipsToBounds
property to NO
.
After that your scroll view pages exactly how you want and you see what's left and right outside the frame.
Additionally you have to do a trick to make this left and right area delegate touches to the scroll view.
在此答案中对此进行了很大的解释.
It's greatly explained in this answer.
这篇关于iOS:如何在UIScrollView中进行正确的分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!