2016-12-20 41 views
0

我有Viewcontroller A上的四個圖像。點擊這些圖像Viewcontroller BUIScrollView呈現圖像視圖,它顯示所有這四個imgaes .... 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); 

     } 


} 
+0

利用UICollectionView,使UICollectionView單元格覆蓋UICollectionView的框架,將UICollectionView滾動條設置爲水平,傳遞圖像數組作爲UICollectionView數據源,最後爲了加載圖像用戶點擊使用collectionView方法scrollToItem(at:index)在viewDidLoad中將scrollToItem的animate屬性設置爲false :) –

+0

忘記提起在UICollectionView上啓用分頁:D –

+0

但是,這將如何解決我用戶點擊圖像2的問題,並且在Viewcontroller B上加載的第一個圖像是圖像2,然後是圖像3,圖像4? – TestShroff

回答

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

}