使用水平滚动在图像视图上加载图像 [英] Load images on a image view using horizontal scrolling

查看:156
本文介绍了使用水平滚动在图像视图上加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过字符串在 UIImageView 上加载图像,我想要水平滚动来查看 UIImageView
在我的xib文件中,我有一个滚动视图,其中有一个图像视图。我正在使用此代码,但我的页面没有滚动,只有字符串中的第一个图像被加载。任何人都可以帮忙。

I'm loading images on the UIImageView through a string and i want horizontal scrolling to view the images on a UIImageView. In my xib file I have a scroll view over which there is a image view. I'm using this code but my page is not scrolling and only the first image in the string is loaded.Can anyone please help.

代码:

-(void)viewDidLoad
    {
        count=1;
       // imagesName = [[NSMutableArray alloc]initWithObjects :@"election_band_base.jpg", @"ElectionBoxAPPA-Hindi(1).jpg", @"photos.png", @"business.png", @"health.png", nil];

         imagesName1 = [NSString stringWithFormat :@"election_band_base.jpg", @"ElectionBoxAPPA-Hindi(1).jpg", @"photos.png", @"business.png", @"health.png", nil];

        [imagesName addObject:imagesName1];

                items = [[NSMutableArray alloc]init];


        // [_imageView1.image setImage=imagesName1];

        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
       // _imageView1.image=[UIImage animatedImageWithImages:imagesName duration:0];

        _imageView1.image=[UIImage imageNamed:imagesName1 ];

        [self loadScrollView];

    }



        -(void)loadScrollView

        {

            scrollView.contentSize = CGSizeMake(0, scrollView.frame.size.height);



            NSMutableArray *controllers = [[NSMutableArray alloc] init];

            for (unsigned i = 0; i < [imagesName count]; i++) {

                [controllers addObject:[NSNull null]];

            }

            self.viewControllers = controllers;
            count=1;


            // a page is the width of the scroll view

            scrollView.pagingEnabled = YES;

            scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [imagesName count], scrollView.frame.size.height);
            //scrollView.contentSize = CGSizeMake(900,80);


            scrollView.showsHorizontalScrollIndicator =YES;

            scrollView.showsVerticalScrollIndicator = YES;

            scrollView.scrollsToTop = NO;

            scrollView.delegate = self;


            pageControl.numberOfPages = [imagesName count];

            pageControl.currentPage = 0;





            // pages are created on demand

            // load the visible page

            // load the page on either side to avoid flashes when the user starts scrolling

            [self loadScrollViewWithPage:0];

            [self loadScrollViewWithPage:1];

        }







       - (void)loadScrollViewWithPage:(int)page {

            if (page < 0) return;

            if (page >= [imagesName count])

                return;


            // replace the placeholder if necessary

            controller = [viewControllers objectAtIndex:page];

            if ((NSNull *)controller == [NSNull null]) {

                NSString *deviceType = [UIDevice currentDevice].model;

                if([deviceType isEqualToString:@"iPhone"])

                {

                    controller = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];

                }

                else{

                    controller = [[MyViewController alloc] initWithNibName:@"MyViewController_ipad" bundle:nil];

                }

                [controller initWithPageNumber:page];

                [controller setArrData:imagesName];

                [viewControllers replaceObjectAtIndex:page withObject:controller];



            }


            // add the controller's view to the scroll view

            if (nil == controller.view.superview) {

                CGRect frame = scrollView.frame;

                frame.origin.x = frame.size.width * page;

                frame.origin.y = 0;

                controller.view.frame = frame;

                [scrollView addSubview:controller.view];

            }

        }

        - (void)unloadScrollViewWithPage:(int)page {

            if (page < 0) return;

            if (page >= [imagesName count]) return;



            controller = [viewControllers objectAtIndex:page];



            if ((NSNull *)controller != [NSNull null]) {

                if (nil != controller.view.superview)

                    [controller.view removeFromSuperview];



                [viewControllers replaceObjectAtIndex:page withObject:[NSNull null]];



            }

        }



        - (void)scrollViewDidScroll:(UIScrollView *)sender {

            // We don't want a "feedback loop" between the UIPageControl and the scroll delegate in

            // which a scroll event generated from the user hitting the page control triggers updates from

            // the delegate method. We use a boolean to disable the delegate logic when the page control is used.

            if (pageControlUsed) {

                // do nothing - the scroll was initiated from the page control, not the user dragging

                return;

            }


            // Switch the indicator when more than 50% of the previous/next page is visible

            CGFloat pageWidth = scrollView.frame.size.width;

            int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

            pageControl.currentPage = page;

            //    NSLog(@"current page %d",page);


            // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)

            [self unloadScrollViewWithPage:page - 2];

            [self loadScrollViewWithPage:page - 1];

            [self loadScrollViewWithPage:page];

            [self loadScrollViewWithPage:page + 1];

            [self unloadScrollViewWithPage:page + 2];

            count=page+1;




            // A possible optimization would be to unload the views+controllers which are no longer visible

        }



        // At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl

        - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollViewLoc

        {

            CGFloat pageWidth = scrollViewLoc.frame.size.width;

            CGPoint translation = [scrollViewLoc.panGestureRecognizer translationInView:scrollViewLoc.superview];

            int page = floor((scrollViewLoc.contentOffset.x - pageWidth / 2) / pageWidth) + 1;





                               }



        // At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl

        - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

            pageControlUsed = NO;

        }

- (IBAction)changePage:(id)sender
{
    int page = pageControl.currentPage;


            // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)

            [self loadScrollViewWithPage:page - 1];

            [self loadScrollViewWithPage:page];

            [self loadScrollViewWithPage:page + 1];



            // update the scroll view to the appropriate page

            CGRect frame = scrollView.frame;

            frame.origin.x = frame.size.width * page;

            frame.origin.y = 0;

            [scrollView scrollRectToVisible:frame animated:YES];



            // Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.

            pageControlUsed = YES;



        }


推荐答案

所以,你想要构建图库视图:

So, you want to built the gallery view:

对于这个工具,需要执行以下步骤:

For this implement the following steps:


  1. UIScrollView 添加到视图控制器的视图中。

  1. Add the UIScrollView to the view of your view controller.

viewDidLoad 方法:在ImageArray中加载图像的名称。 (我假设你的所有图像的名称都是img1.png,img2.png,img3.png,....)

In viewDidLoad method: Load the name of the images in the "ImageArray". (I assume all your images have names as "img1.png", "img2.png", "img3.png", ....)

ImageArray=[[NSMutableArray alloc]init];

for (int i=0; i<19; i++) {
   NSString *imgtext=[[NSString alloc]initWithFormat:@"img%d",i+1];
   [ImageArray addObject:imgtext];
}


  • viewWillAppear 方法,添加以下代码:

  • In the viewWillAppear method, add the following code:

    for (int i = 0; i < ImageArray.count; i++) {
    CGRect frame;
    frame.origin.x = self.scrollview.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.scrollview.frame.size;
    UIView *subview = [[UIView alloc] initWithFrame:frame];
    
    UIImage *image = [UIImage imageNamed: [NSString stringWithFormat:@"%@.png",[ImageArray objectAtIndex:i]]];
    UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
    [imageView setFrame:CGRectMake(0, 0, frame.size.width,frame.size.height )];
    [subview addSubview:imageView]; 
    [self.scrollview addSubview:subview];
     }
    
    self.scrollview.contentSize = CGSizeMake(self.scrollview.frame.size.width * ImageArray.count, self.scrollview.frame.size.height);
    
    self.scrollview.contentOffset=CGPointMake (self.scrollview.frame.size.width, 0);
    


  • 希望有所帮助。

    这篇关于使用水平滚动在图像视图上加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆