2

i am newBie in iOS Development.i make an ScrollView it contain my JSOn Array Images and my Scrollview have Paging is Enabled and i add a method for Scrollview to zoom viewForZoomingInScrollView then it is zoom my First image and Overlapped on Second image i want to zoom only my Selected image when viewForZoomingInScrollView method Called here my Code for Scrollview and For viewForZoomingInScrollView method is

Code for Scrollview to add images

for( index=0; index < [self.imagesa count]; index++)
{
    NSDictionary *dict=[self.imagesa objectAtIndex:index];
    NSString *image=[dict valueForKey:@"link"];
    bigImage=[[UIImageView alloc]init];
    bigImage.userInteractionEnabled=TRUE;
    bigImage.image=nil;
    bigImage.bounds=CGRectMake(0, 0, self.zoomScroll.frame.size.width, self.zoomScroll.frame.size.height);
    bigImage.frame=CGRectMake(index * self.zoomScroll.frame.size.width, 0, self.zoomScroll.frame.size.width, self.zoomScroll.frame.size.height);
    [bigImage setMultipleTouchEnabled:YES];
    [bigImage removeFromSuperview];
    [bigImage sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:@"1.png"]];
    [bigImage setUserInteractionEnabled:TRUE];
    [self.objectarray insertObject:bigImage atIndex:index];
    [self.zoomScroll addSubview:bigImage];
    self.zoomScroll.clipsToBounds=YES;
    [self.zoomScroll addSubview:[self.objectarray objectAtIndex:index]];
}
[self.zoomScroll addSubview:bigImage];

Here self.imagesa is Json Parsed array and self.imagearray is NSMuttable array.

And Zooming Method

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
for (index=0 ; index <[self.imageArray count]; index ++)
{
    return [self.zoomScroll.subviews objectAtIndex:index];
}
return nil;
}

But it is Not Working As Right it is zoom Only First Image and When Zoom First Image and I scroll My Scrollview then Second Image is Overlaped. and here i also Set Maximum and Minimum Scale for Scrollview.And when i ZoomScrollview then its Size is Change and Only Show First image and Not Scroll.

4 Answers4

2

Please check if this is useful for you, as for my app requirement I am using this.

First thing if you are adding imageViews into scrollView, then before adding each imageView into scrollView set the value for tag property of each imageView it is ok if you set same value for tag property of all the imageViews you are going to add in scrollView.

Then add UIScrollView's delegate method as follows and check,

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{
    return [scrollView viewWithTag:<valueOfTagPropertySetToImageView>];
}
Kanhaiya
  • 249
  • 1
  • 2
  • 8
0

just used the methods like below:-

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

 {

   return self.imageView;

 }
- (void)viewDidLoad {

    [super viewDidLoad];

     self.scrollView.minimumZoomScale=0.5;

     self.scrollView.maximumZoomScale=6.0;

     self.scrollView.contentSize=CGSizeMake(1280, 960);

     self.scrollView.delegate=self;

    }

one more method of zoom:-

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view   atScale:(float)scale
   {}
Rameshwar Gupta
  • 791
  • 7
  • 21
0

To Achieve your requirement you have to make some change in your current structure.

Currently you are adding all ImageViews to a single ScrollView, instead of that you have to create ScrollViews for each of your ImageView and add all ScrollViews to single ScrollView.

Your code should look like :

UIScrollView *parentScrollView = [[UIScrollView all] initWithFrame:<frame>];

for( index=0; index < [self.imagesa count]; index++)
{
    UIScrollView *innerScrollView = [UIScrollView alloc] initWithFrame:CGRectMake(index * parentScrollView.frame.size.width, 0, parentScrollView.frame.size.width, parentScrollView.frame.size.height)];

    NSDictionary *dict=[self.imagesa objectAtIndex:index];
    NSString *image=[dict valueForKey:@"link"];
    bigImage=[[UIImageView alloc]init];
    bigImage.tag = 123; // Helps to get ImageView to given as zoom view in scrollview delegate
    bigImage.userInteractionEnabled=TRUE;
    bigImage.image=nil;
    bigImage.frame=CGRectMake(0, 0, innerScrollView.frame.size.width, innerScrollView.frame.size.height);
    [bigImage setMultipleTouchEnabled:YES];
    [bigImage removeFromSuperview];
    [bigImage sd_setImageWithURL:[NSURL URLWithString:image] placeholderImage:[UIImage imageNamed:@"1.png"]];
    [bigImage setUserInteractionEnabled:TRUE];
    [self.objectarray insertObject:bigImage atIndex:index];

    [parentScrollView addSubview:innerScrollView]; 
}

Your Zooming method may look like

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    UIImageView *imageView = [scrollView viewWithTag:123]; // returns ImageView
    if(imageView){
        return imageView;
    }
    return nil;
}
Yuvrajsinh
  • 4,536
  • 1
  • 18
  • 32
  • here write your code then in viewForZoomingInScrollView method error is incompatible pointer type initialise "UIImageView" with an expression of type'UIView' –  Nov 14 '14 at 05:30
  • Is it warning or error? As I have not tested my code but returning UIImageView should not give error but may be a warning. OR you can cast UIImageView to UIView like return (UIView *)imageView – Yuvrajsinh Nov 14 '14 at 05:34
  • it is not Show my Scrollview when i run application. –  Nov 14 '14 at 05:37
  • and it is not scroll when i add a subscrollview to my Scrollview. –  Nov 14 '14 at 05:40
  • I have not added parentScrollView as subview to self.view you have to do that and set contentSize of parentScrollView to enable scrolling, I have only answered how you can achieve your result there might be some configurations needed. – Yuvrajsinh Nov 14 '14 at 05:46
  • i will try it. and check it –  Nov 14 '14 at 05:52
  • i get only First index data and not able to zoom when i edit some extra code. –  Nov 14 '14 at 06:44
0

Check My answer from this link ,

Check the link

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

{

    return self.imageView;

}
- (void)viewDidLoad {

    [super viewDidLoad];

    self.scrollView.minimumZoomScale=0.5;

    self.scrollView.maximumZoomScale=6.0;

    self.scrollView.contentSize=CGSizeMake(1280, 960);

    self.scrollView.delegate=self;

}

Solution from Apple itself , Apple Documentation link

Community
  • 1
  • 1
Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
  • Have you connect the ScrollView Delegate in storyboard. – Vineesh TP Nov 14 '14 at 10:47
  • Yes @Vineesh TP i connect all Delegate. –  Nov 14 '14 at 10:50
  • But here i think it is not working because my ScrollView Contain image Array. –  Nov 14 '14 at 10:50
  • @AshishGabani: add only one image. I have already given the apple documentation. Atleast read the document. – Vineesh TP Nov 14 '14 at 11:05
  • I know in apple document also added only one image but i want all my array images in to my scrollview please give me solution if you have. –  Nov 14 '14 at 11:10
  • Add paging for scrollView, the you can display all images on the scrollView itself while dragging the scrollView. – Vineesh TP Nov 14 '14 at 11:37