UIscrollView 水平滚动图像并显示单击图像中的图像

Posted

技术标签:

【中文标题】UIscrollView 水平滚动图像并显示单击图像中的图像【英文标题】:UIscrollView To scroll images horizontally and show images from the image clicked 【发布时间】:2016-12-19 09:13:53 【问题描述】:

我在视图控制器上有四个图像。单击这些图像会打开newViewController,即LargeImageViewController。在 LargeImageViewController 上有 ScrollView 可以进行水平滚动。单击每个按钮时,LargeImageViewController 上的图像从 image1 开始,然后显示 image2,然后是图像 3,然后是图像 4。

我希望如果单击图像 2,则 LargeImageViewController 上的图像应该启动 image2,然后是图像 3,然后是图像 4.....但是当它转到上一个图像时,它应该显示图像 4、图像 3、图像 2还有 image1。

如何做到这一点??

我使用的代码如下:

- (void)viewDidLoad 
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   // int pageCount=4;
   NSArray *imgArray = [self.tripDetails valueForKey:@"Flightimageurl"];
    width = [UIScreen mainScreen].bounds.size.width;
    height = [UIScreen mainScreen].bounds.size.height;
    _scroller = [[UIScrollView alloc]initWithFrame:
                 CGRectMake(0,64,width,height)];
      _scroller.contentSize=CGSizeMake([imgArray count]*_scroller.bounds.size.width,_scroller.bounds.size.height);
    CGRect ViewSize=_scroller.bounds;
    for(int i=0;i<[imgArray count];i++)
    
        UIImageView *imgView1=[[UIImageView alloc]initWithFrame:ViewSize];
        NSString *ImageURL = [imgArray objectAtIndex:i];
        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];

        imgView1.image=[UIImage imageWithData:imageData];
        [_scroller addSubview:imgView1];
        [self.view addSubview:_scroller];
        ViewSize =CGRectOffset(ViewSize,_scroller.bounds.size.width,0);

    


请帮助提出更改建议。

【问题讨论】:

我能帮到你。 ***.com/questions/31220071/… 不,不导入库可以吗?? 是的,有可能.. 如何..请解释一下。 【参考方案1】:
-(void)singleTapping:(UIGestureRecognizer *)recognizer 

int imageTag = (int) recognizer.view.tag;

NSDictionary *dictCurrentWish = [arrLatestScrollData objectAtIndex:pageNumberSaved];

scrollimagePostView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, kSCREEN_WIDTH, kSCREEN_HEIGHT)];

scrollimagePostView.pagingEnabled=YES;
scrollimagePostView.delegate=self;


UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[scrollimagePostView addGestureRecognizer:gr];

NSMutableArray *arrTotalImages = [[NSMutableArray alloc]initWithCapacity:0];

[arrTotalImages addObject:[dictCurrentWish objectForKey:@"pic1"]];
[arrTotalImages addObject:[dictCurrentWish objectForKey:@"pic2"]];
[arrTotalImages addObject:[dictCurrentWish objectForKey:@"pic3"]];
[arrTotalImages addObject:[dictCurrentWish objectForKey:@"pic4"]];

int x=0;

CGRect innerScrollFrame = scrollimagePostView.bounds;

for (int i=0; i<arrTotalImages.count; i++) 

    imgViewPost=[[UIImageView alloc]initWithFrame:CGRectMake(x, 60, kSCREEN_WIDTH,kSCREEN_HEIGHT-90)];


    NSString *strImage  =[NSString stringWithFormat:@"%@", [arrTotalImages objectAtIndex:i]];


    NSString *strURL=[strImage stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

    NSURL* urlAddress1 = [[NSURL alloc] initWithString:strURL];

    [imgViewPost  sd_setImageWithURL:urlAddress1 placeholderImage:wishPlaceHolderImage];

    imgViewPost.contentMode = UIViewContentModeScaleAspectFit;

    imgViewPost.tag = VIEW_FOR_ZOOM_TAG;

    UIScrollView *pageScrollView = [[UIScrollView alloc]
                                    initWithFrame:innerScrollFrame];
    pageScrollView.minimumZoomScale = 1.0f;
    pageScrollView.maximumZoomScale = 6.0f;
    pageScrollView.zoomScale = 1.0f;
    pageScrollView.contentSize = imgViewPost.bounds.size;
    pageScrollView.delegate = self;
    pageScrollView.showsHorizontalScrollIndicator = NO;
    pageScrollView.showsVerticalScrollIndicator = NO;
    [pageScrollView addSubview:imgViewPost];
    [scrollimagePostView addSubview:imgViewPost];

    x=x+kSCREEN_WIDTH;

    if (i < 2) 
        innerScrollFrame.origin.x += innerScrollFrame.size.width;
    



scrollimagePostView.contentSize = CGSizeMake(x, scrollimagePostView.frame.size.height );

scrollimagePostView.backgroundColor = [UIColor blackColor];
[self.view addSubview:scrollimagePostView];


[scrollimagePostView setContentOffset:CGPointMake(scrollimagePostView.frame.size.width*(imageTag-1), 0.0f) animated:NO];


btnCloseFullIMageView = [[UIButton alloc]initWithFrame:CGRectMake(kSCREEN_WIDTH-80, 25, 70, 25)];
[btnCloseFullIMageView setTitle:@"Close" forState:UIControlStateNormal];
[btnCloseFullIMageView setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
btnCloseFullIMageView.backgroundColor = [UIColor blackColor];
btnCloseFullIMageView.layer.borderColor = [UIColor whiteColor].CGColor;
btnCloseFullIMageView.layer.borderWidth = 0.5;
btnCloseFullIMageView.layer.cornerRadius = 3.0;
btnCloseFullIMageView.clipsToBounds = TRUE;

[btnCloseFullIMageView addTarget:self action:@selector(closeFullImageView:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:btnCloseFullIMageView];


【讨论】:

以上是关于UIscrollView 水平滚动图像并显示单击图像中的图像的主要内容,如果未能解决你的问题,请参考以下文章

使用 UIScrollView 在多个图像之间水平滚动

全景图上的水平 UIScrollView

单击按钮滚动 UIScrollView

区分点击和滚动 - iOS

UIScrollview在垂直和水平ios中滚动

带有“单元格”的水平 UIScrollView?