在ios中的图像滚动查看
您好我的要求是scrollview
显示图像,应显示下一个和上一个button。用户可以滚动图像以及使用button来移动图像。
我怀疑如何获得index
/ tag
/图像名称或其他任何东西时,基于该值的button滚动应更新。
这是我的代码
[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 {}
滚动工作正常,但当用户点击上一个和下一个图像不更新'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); }
我有滚动图像的左右button,和imageview的宽度是相同的。
我正在编写滚动图像的代码在按下button。
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]; } }
设置scrollView Deleagte
滚动查看委托方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { pos=scrollView.contentOffset.x/scrollView.frame.size.width; }
您可以使用UIPageControl并根据页面索引设置和获取图像。 欲了解更多信息,请查看此链接
尝试下面的一段代码。 我希望这会有所帮助。 把它放在你的.h文件中
IBOutlet UIScrollView *scrollView; IBOutlet UIPageControl *pageControl; NSMutableArray *arrImages; NSInteger page;
现在在你的 – (void)viewDidLoad中join以下几行。
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 } } }
- Facebook UIActivityViewController缺less取消和发布button
- iOS为UIImageView获取图像的可见部分
- 如何在iOS编程中检查网络提供商名称?
- 支持iPhone 6和iPhone 6以及不同的启动/闪屏图像,适用于iPad纵向和横向方向
- iOS:简单MVC模型中的内存泄漏
- UIWebView中的Facebook身份validation不会重定向回我站点上的原始页面,要求进行身份validation
- 使NSString中的某些单词加粗并可点击标签
- iOS7只返回’NSInvalidArgumentException’,原因:’ – :
- 应用处于后台模式时的文字转语音function?