根据单击的图像在水平滚动视图上的图像

Posted

技术标签:

【中文标题】根据单击的图像在水平滚动视图上的图像【英文标题】:Images on horizontal scroll view according to clicked image 【发布时间】:2016-12-20 07:50:44 【问题描述】:

我在Viewcontroller A 上有四个图像。单击这些图像Viewcontroller B 具有UIScrollView 具有图像视图的呈现,它显示所有这四个图像....图像1,图像2,图像3,图 4。 我希望当单击图像 2 时,图像 2 作为 Viewcontroller B 上的第一张图像出现,然后是图像 3,然后是图像 4...此外,当用户向左移动时,它也会显示以前的图像,包括图像 1。

我搜索了很多,但找不到这个问题的解决方案Kindly.help

我使用的代码如下:

- (void)viewDidLoad 
    [super viewDidLoad];

  width = [UIScreen mainScreen].bounds.size.width;
    height = [UIScreen mainScreen].bounds.size.height;
    _scroller = [[UIScrollView alloc]initWithFrame:
                 CGRectMake(0,64,width,height)];
      _scroller.contentSize=CGSizeMake(pageCount*_scroller.bounds.size.width,_scroller.bounds.size.height);
    _scroller.pagingEnabled=YES;
    _scroller.showsHorizontalScrollIndicator=YES;
    CGRect ViewSize=_scroller.bounds;

    NSArray *imgArray = [self.tripDetails valueForKey:@"Flightimageurl"];


        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);

        



【问题讨论】:

利用UICollectionView,使UICollectionView单元格覆盖UICollectionView的边框,设置UICollectionView水平滚动,将图像数组作为UICollectionView数据源传递,最后为了加载用户点击的图像使用collectionView方法viewDidLoad 中的 scrollToItem(at: index) 将 scrollToItem 的动画属性设置为 false :) 忘了在 UICollectionView 上启用分页:D 但是这将如何解决我的用户点击图像 2 并且在 Viewcontroller B 上加载的第一张图像是图像 2 然后是图像 3、图像 4 的问题? 【参考方案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 水平滚动图像并显示单击图像中的图像

scrollView 上的触摸功能

如何使用不同大小和方向的图像创建水平滚动视图?

滚动列表视图垂直和水平相同的图像

SwiftUI:水平滚动视图我能够上下拉图像

如何更改水平滚动视图中图像按钮的宽度?