2
votes

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

4 Answers

2
votes

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>];
}
0
votes

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
   {}
0
votes

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;
}
0
votes

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