ios中图像的ScrollView
Posted
技术标签:
【中文标题】ios中图像的ScrollView【英文标题】:ScrollView of images in ios 【发布时间】:2014-05-12 06:52:11 【问题描述】:您好,我的要求是在scrollview
中显示图像,并显示下一个和上一个按钮。用户可以滚动图像以及使用按钮移动图片。
我对如何获取index
/tag
/imagename
或当用户滚动基于该值的按钮应该更新时的任何其他内容存有疑问。
这是我的代码
[self arrayInitiaisation];
scrollview.pagingEnabled = YES;
scrollview.alwaysBounceVertical = NO;
scrollview.alwaysBounceHorizontal = YES;
NSInteger numberOfViews = 4;
for (int i = 0; i < numberOfViews; i++)
CGFloat xOrigin = i*320;
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image%d",i+1]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollview addSubview:imageView];
imageView.tag = i;
...
数组初始化方法
-(void) arrayInitialisation
count=0;
[super viewDidLoad];
images=[NSMutableArray arrayWithObjects : [UIImage imageNamed:@"image1.png"],
[UIImage imageNamed :@"image2.png"],
[UIImage imageNamed :@"image3.png"],
[UIImage imageNamed :@"image4.png"],nil];
[imageView setImage:[images objectAtIndex:0]];
NSLog(@"Array Length :%d",images.count);
[previous setEnabled:NO];
动作方法
- (IBAction)nextButton:(id)sender
- (IBAction)prevButton:(id)sender
滚动工作正常,但是当用户点击 prev 并且 next 图像没有在 'imageview' 上更新
这是我更新的代码
int count=0;
- (void)viewDidLoad
[super viewDidLoad];
[self arrayInitialisation];
[scrollview setScrollEnabled:YES];
self.scrollview =[[UIScrollView alloc] initWithFrame:CGRectMake(30 , 30, 320, 412)];
self.scrollview.delegate=self;
scrollview.pagingEnabled = YES;
scrollview.alwaysBounceVertical=NO;
scrollview.alwaysBounceHorizontal=YES;
scrollview.showsHorizontalScrollIndicator=NO;
NSInteger numberOfViews = 4;
for (int i = 0; i < numberOfViews; i++)
CGFloat xOrigin = i*320;
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(xOrigin, 0, 320, 412)];
imageView.image = [images objectAtIndex:i];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollview addSubview:imageView];
imageView.tag=i;
scrollview.contentSize = CGSizeMake(320*numberOfViews, 412);
scrollview.contentOffset = CGPointMake(0,0);
[self.view addSubview:self.scrollview];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
-(void) arrayInitialisation
count=0;
[super viewDidLoad];
images=[NSArray arrayWithObjects : [UIImage imageNamed:@"image1.png"],
[UIImage imageNamed :@"image2.png"],
[UIImage imageNamed :@"image3.png"],
[UIImage imageNamed :@"image4.png"],nil];
[imageView setImage:[images objectAtIndex:0]];
NSLog(@"Array Length :%d",images.count);
[previous setEnabled:NO];
- (IBAction)nextButton:(id)sender
if(count==0)
count++;
NSLog(@"Next Button : if Loop %d",count);
[imageView setImage:[images objectAtIndex:count]];
[previous setEnabled:YES];
else
[previous setEnabled:YES];
count++;
NSLog(@"Next Button : else Loop %d",count);
if(count <images.count)
[imageView setImage:[images objectAtIndex:count]];
else
[next setEnabled:NO];
- (IBAction)prevButton:(id)sender
if(count==0)
//count--;
NSLog(@"prev Button : if Loop %d",count);
[imageView setImage:[images objectAtIndex:count]];
[previous setEnabled:NO];
[next setEnabled:YES];
else
count--;
NSLog(@"prev Button : else Loop %d",count);
[imageView setImage:[images objectAtIndex:count]];
[previous setEnabled:YES];
//[next setEnabled:YES];
#pragma mark - ScrollView Delegate
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate: (BOOL)decelerate
//NSLog(@"scrollViewDidEndDragging");
/* count=count+1;
NSLog(@"scrollViewDidEndDragging : %d",count);*/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//NSLog(@"scrollViewDidScroll");
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_ios(5_0)
//NSLog(@"scrollViewWillEndDragging");
count=count+1;
NSLog(@"scrollViewWillEndDragging : %d",count);
【问题讨论】:
github.com/nicklockwood/iCarousel 感谢@Waki 的链接 【参考方案1】:我有滚动图像的左右按钮,并且图像视图宽度相同。
我正在编写按下按钮时滚动图像的代码。
int pos=0;// ViewDiaLoad
- (IBAction)OnClickLeftButton:(UIButton*)sender
if(pos>0)
pos -=1;
[scrlDemandMenu scrollRectToVisible:CGRectMake(pos*scrlDemandMenu.frame.size.width, 0, scrlDemandMenu.frame.size.width, scrlDemandMenu.frame.size.height) animated:YES];
- (IBAction)OnClickRightButton:(UIButton*)sender
if(pos<numberOfImages)
pos +=1;
[scrlDemandMenu scrollRectToVisible:CGRectMake(pos*scrlDemandMenu.frame.size.width, 0, scrlDemandMenu.frame.size.width, scrlDemandMenu.frame.size.height) animated:YES];
设置滚动视图委托
滚动视图委托方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
pos=scrollView.contentOffset.x/scrollView.frame.size.width;
【讨论】:
谢谢。我的问题已经解决了。@Bipin Patel【参考方案2】:您可以使用 UIPageControl 并根据页面索引设置和获取图像。欲了解更多信息,请查看此link
【讨论】:
我的要求是只使用按钮来导航图像。 @Satyarth Kumar Prasad 图片滚动时如何获取contentOffset。我需要计算图片是从左到右滚动还是从左到右滚动。 - (void)scrollViewDidEndDecelerating:(UIScrollView *)sender uint page = sender.contentOffset.x / width; [self.pageControl setCurrentPage:page]; 【参考方案3】:试试下面的代码。我希望它会有所帮助。把它放在你的 .h 文件中
IBOutlet UIScrollView *scrollView;
IBOutlet UIPageControl *pageControl;
NSMutableArray *arrImages;
NSInteger page;
现在在您的 - (void)viewDidLoad 中添加以下几行。
int x = 30;
for (int i=0; i< arrImages.count; i++)
UIImageView *imgShow = [[UIImageView alloc]initWithFrame:CGRectMake(x+20, 50, self.view.frame.size.width-100, self.view.frame.size.height-180)];
imgShow.backgroundColor = [UIColor whiteColor];
imgShow.image = [UIImage imageNamed:[arrImages objectAtIndex:i]];
imgShow.contentMode = UIViewContentModeScaleAspectFit;
[scrollView addSubview:imgShow];
x = x+scrollView.frame.size.width;
scrollView.contentSize = CGSizeMake(5*self.view.frame.size.width, 0);
pageControl.backgroundColor = [UIColor clearColor];
pageControl.pageIndicatorTintColor = [UIColor grayColor];
pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
[pageControl setTintAdjustmentMode:UIViewTintAdjustmentModeDimmed];
pageControl.numberOfPages = arrTitle.count;
pageControl.currentPage = 0;
实现滚动视图代理。
#pragma mark - ScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView1
CGFloat pageWidth = scrollView.frame.size.width;
float fractionalPage = scrollView.contentOffset.x / pageWidth;
page = lround(fractionalPage);
pageControl.currentPage = page;
- (IBAction)prevButton:(UIButton *)sender
page = page-1;
UIButton *btn = (UIButton *)[sender viewWithTag:sender.tag];
// write your code
for (id obj in btn.subviews)
if ([obj isKindOfClass:[UIImageView class]])
// write your code
else
// write your code
- (IBAction)nextButton:(UIButton *)sender
page = page+1;
UIButton *btn = (UIButton *)[sender viewWithTag:sender.tag];
// write your code
for (id obj in btn.subviews)
if ([obj isKindOfClass:[UIImageView class]])
// write your code
else
// write your code
【讨论】:
如何使用 contentOffset 更改 scroview 中的图像以上是关于ios中图像的ScrollView的主要内容,如果未能解决你的问题,请参考以下文章