水平滚动视图上的图片根据点击的图片

时间:2016-12-20 07:50:44

标签: ios objective-c uiscrollview

我在Viewcontroller A上有四张图片。点击那些Viewcontroller B图片UIScrollView的图片会显示图片视图,并显示所有这四个图片.... Image1,图像2,图像3,图像4。 我希望在单击图像2时,图像2显示为Viewcontroller B上的第一个图像,然后是图像3,然后是图像4 ...此外,当用户向左移动时,它也会显示包含image1的先前图像。

我搜索了很多但找不到解决此问题的方法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);

        }


}

1 个答案:

答案 0 :(得分:0)

使用此代码对您有所帮助

-(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];

}