2016-12-19 117 views
1

我在視圖控制器上有四個圖像。在點擊這些圖像時,newViewController即打開LargeImageViewController。在LargeImageViewController有滾動查看水平滾動。點擊每個按鈕,LargeImageViewController上的圖像從image1開始,然後顯示image2,然後顯示圖像3,然後顯示圖像4.UIscrollView水平滾動圖像並顯示點擊圖像的圖像

我希望如果單擊圖像2,那麼LargeImageViewController上的圖像應該啓動image2,然後是圖像3,然後圖像4 .....但是當它轉到先前的圖像時,它應該顯示圖像4,圖像3,圖像2和圖像1。

這是如何實現的?我使用

代碼如下:

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

    } 

} 

與建議的更改請幫助。

+0

我是這可以幫助你。 http://stackoverflow.com/questions/31220071/how-to-create-image-slider-in-ios – PiyushRathi

+0

不,是否有可能沒有導入庫? – TestShroff

+0

是的,這是可能的。 – PiyushRathi

回答

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

}