UIscrollview放入大图滑动and捏合放大缩小

如何把一张大图片放入scrollview可以滑动显示

首先在storyboard里拖入一个UIscrollview

  代码:

    self.myScrollView.contentSize = CGSizeMake(1000, 667);    //里面content的size大小决定图片显示大小
    
    UIImage *image1 = [UIImage imageNamed:@"111.jpg"];
    
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 1000, 667)];
    
    imageview.image = image1;
    
    [self.myScrollView addSubview:imageview];


捏合放大缩小

#import

@interface YcwViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIScrollView *myScrollView;

- (void)scrollViewDoubleTapped:(UITapGestureRecognizer *)recognizer;

- (void)scrollTwoFingerTapped:(UITapGestureRecognizer *)recognizer;

@end

#import "YcwViewController.h"
#define ZOOM_STEP 1.5f
@interface YcwViewController ()
{
    UIImageView *imageView;
}
@end



@implementation YcwViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    CGRect scrollViewFrame = self.myScrollView.frame;
    CGFloat scaleWidth = scrollViewFrame.size.width/self.myScrollView.contentSize.width;
    
    CGFloat scaleHeight = scrollViewFrame.size.height/self.myScrollView.contentSize.height;
    
    CGFloat minScale = MAX(scaleWidth,scaleHeight);
    
    self.myScrollView.maximumZoomScale = 2.0f;
    self.myScrollView.minimumZoomScale = minScale;
    self.myScrollView.zoomScale = minScale;

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.myScrollView.delegate = self;
    
    UIImage *image = [UIImage imageNamed:@"222.jpg"];
    
    imageView = [[UIImageView alloc]initWithImage:image];
    
    
    
    imageView.userInteractionEnabled = YES;
    
    [self.myScrollView addSubview:imageView];
    
    self.myScrollView.contentSize = image.size;
    

   //注册手势(一手指轻按2次)
    UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer  alloc]initWithTarget:self action:@selector(scrollViewDoubleTapped:)];
    
    doubleTapRecognizer.numberOfTapsRequired = 2;
    doubleTapRecognizer.numberOfTouchesRequired = 1;
    
    [self.myScrollView addGestureRecognizer:doubleTapRecognizer];
    
   //注册手势 (二个手指轻按一次)
    UITapGestureRecognizer *twoFingerTapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(scrollTwoFingerTapped:)];
    
    twoFingerTapRecognizer.numberOfTapsRequired = 1;
    
    twoFingerTapRecognizer.numberOfTouchesRequired = 2;
    
    [self.myScrollView addGestureRecognizer:twoFingerTapRecognizer];
    
}



- (void)scrollViewDoubleTapped:(UITapGestureRecognizer *)recognizer{

    CGPoint pointView = [recognizer locationInView:recognizer.view];
    CGFloat newZoomScale = self.myScrollView.zoomScale * ZOOM_STEP;
    
    CGSize scrollViewSize = self.myScrollView.frame.size;
    
    CGFloat width = scrollViewSize.width /newZoomScale;
    CGFloat heigh = scrollViewSize.height /newZoomScale;
    CGFloat x = pointView.x - (width/2.0f);
    CGFloat y = pointView.y - (heigh/2.0f);
    
    CGRect rectToZoomTo = CGRectMake(x,y, width, heigh);
    
    [self.myScrollView zoomToRect:rectToZoomTo animated:YES];
    
    
}

- (void)scrollTwoFingerTapped:(UITapGestureRecognizer *)recognizer{

    CGFloat newZoomScale = self.myScrollView.zoomScale/ZOOM_STEP;
    
    newZoomScale = MAX(newZoomScale, self.myScrollView.minimumZoomScale);
    
    [self.myScrollView setZoomScale:newZoomScale animated:YES];
}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return imageView;
}



你可能感兴趣的:(UIscrollview放入大图滑动and捏合放大缩小)