iOS 选取头像视图的实现(一)

@interface ViewController ()
{
    UIScrollView *_scrollView;
    UIImageView *_imgView;
    CGFloat _deltaW;
    CGFloat _deltaH;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [superviewDidLoad];
    
    UIScrollView *scrollView = [[UIScrollViewalloc] initWithFrame:self.view.bounds];
    [self.viewaddSubview:scrollView];
    _scrollView = scrollView;
    _scrollView.showsHorizontalScrollIndicator =NO;
    _scrollView.showsVerticalScrollIndicator =NO;
    
    UIImageView *imgView = [[UIImageViewalloc] initWithFrame:self.view.bounds];
    imgView.image = [UIImageimageNamed:@"img.png"];
    [scrollView addSubview:imgView];
    _imgView = imgView;
    
    CALayer *maskView = [[CALayeralloc] init];
    maskView.frame =CGRectMake(self.view.bounds.size.width/2 - 75, self.view.bounds.size.height/2 - 75, 150, 150);
    [self.view.layeraddSublayer:maskView];
//    maskView.position = CGPointMake(, );
    maskView.backgroundColor = [UIColorcolorWithWhite:0.5alpha:0.5].CGColor;
    
    _scrollView.maximumZoomScale =2.0;
    // 如果图片的高大于宽用固定值/宽
    // 如果图片的高小于宽用固定值/高
    _scrollView.minimumZoomScale = maskView.bounds.size.width/imgView.bounds.size.width;
    
    // 应该把滑动视图的contentSize的宽和高要加上矩形区域相对于屏幕的的高和宽的差值
    _deltaW =self.view.bounds.size.width - maskView.bounds.size.width;
    _deltaH =self.view.bounds.size.height - maskView.bounds.size.height;
    CGSize contentSize =CGSizeMake(imgView.bounds.size.width + _deltaW, imgView.bounds.size.height + _deltaH);
    _scrollView.contentSize = contentSize;
    _scrollView.delegate =self;
    
    CGFloat centerX = contentSize.width/2.0;
    CGFloat centerY = contentSize.height/2.0;
    imgView.center =CGPointMake(centerX, centerY);
    
    [_scrollViewsetContentOffset:CGPointMake(_deltaW/2.0,_deltaH/2.0)];

    // 手势是添加在scrollview上的
    UITapGestureRecognizer *pinchGes = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(pinchAction:)];
    pinchGes.numberOfTapsRequired =1;
    pinchGes.numberOfTouchesRequired =2;
    [_scrollViewaddGestureRecognizer:pinchGes];
}

- (void)pinchAction:(UITapGestureRecognizer *)pinchGes {
 
}


-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return_imgView;
}
-(void)scrollViewDidZoom:(UIScrollView *)scrollView {

}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
    _scrollView.contentSize =CGSizeMake(_imgView.bounds.size.width*scale + _deltaW,_imgView.bounds.size.height*scale + _deltaH);
}


你可能感兴趣的:(UIScrollView,Table,Collection)