首页 > 代码库 > scrollview里面多张图片,每张都能放大缩小

scrollview里面多张图片,每张都能放大缩小

#import "urltestViewController.h"


@interface urltestViewController ()


@end


@implementation urltestViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

    self.view.backgroundColor = [UIColorwhiteColor];

    NSArray *array = [NSArrayarrayWithObjects:@"111.png",@"222.png",@"333.png",nil];

    

    self.imageScrollView = [[UIScrollViewalloc]initWithFrame:CGRectMake(0,0,320,568)];

    self.imageScrollView.backgroundColor = [UIColorclearColor];

    self.imageScrollView.scrollEnabled =YES;

    self.imageScrollView.pagingEnabled =YES;

    self.imageScrollView.delegate =self;

   self.imageScrollView.contentSize =CGSizeMake(array.count*320,568);

   for (int i =0; i <3; i++){

       UITapGestureRecognizer *doubleTap =[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(handleDoubleTap:)];

        [doubleTap setNumberOfTapsRequired:2];


        UIScrollView *s = [[UIScrollViewalloc]initWithFrame:CGRectMake(320*i,0,320,568)];

         s.backgroundColor = [UIColorclearColor];

         s.contentSize =CGSizeMake(320,568);

         s.delegate =self;

         s.minimumZoomScale =0.5;

         s.maximumZoomScale =3.0;

         //        s.tag = i+1;

         [ssetZoomScale:1.0];

         

        UIImageView *imageview = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,320,568)];

        NSString *imageName = [arrayobjectAtIndex:i];

         imageview.image = [UIImageimageNamed:imageName];

         imageview.userInteractionEnabled =YES;

         imageview.tag = i+1;

         [imageviewaddGestureRecognizer:doubleTap];

         [saddSubview:imageview];

         [self.imageScrollViewaddSubview:s];

        

    }

    

//    self.imageScrollView.contentOffset = CGPointMake(320, 0);

    [self.viewaddSubview:self.imageScrollView];

}


#pragma mark - ScrollView delegate

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

    

   for (UIView *vin scrollView.subviews){

       return v;

    }

    return nil;

}


- (void)viewDidUnload

{

    [superviewDidUnload];

    // Release any retained subviews of the main view.

    _imageScrollView=nil;

    //nameArray =nil;

}

//- (void)dealloc

//{

//    [imageScrollView release];

//    //  [nameArray release];

//    [super dealloc];

//}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    if(interfaceOrientation ==UIInterfaceOrientationPortrait||interfaceOrientation ==UIInterfaceOrientationPortraitUpsideDown)

    {

       returnYES;

    }

    return NO;

}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    

   if (scrollView ==self.imageScrollView){

       CGFloat x = scrollView.contentOffset.x;

       if (x==-333){

            

        }

       else {

//            offset = x;

           for (UIScrollView *sin scrollView.subviews){

               if ([sisKindOfClass:[UIScrollViewclass]]){

                    [s setZoomScale:1.0]; //scrollView每滑动一次将要出现的图片较正常时候图片的倍数(将要出现的图片显示的倍数)

                }

            }

        }

    }

}


#pragma mark -

-(void)handleDoubleTap:(UIGestureRecognizer *)gesture{

    

   float newScale = [(UIScrollView*)gesture.view.superviewzoomScale] * 1.5;//每次双击放大倍数

   CGRect zoomRect = [selfzoomRectForScale:newScalewithCenter:[gesturelocationInView:gesture.view]];

    [(UIScrollView*)gesture.view.superviewzoomToRect:zoomRectanimated:YES];

}


- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center

{

   CGRect zoomRect;

    zoomRect.size.height =self.view.frame.size.height / scale;

    zoomRect.size.width  =self.view.frame.size.width  / scale;

    zoomRect.origin.x = center.x - (zoomRect.size.width  /2.0);

    zoomRect.origin.y = center.y - (zoomRect.size.height /2.0);

   return zoomRect;

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end



scrollview里面多张图片,每张都能放大缩小