//
//  CheckPicViewController.m
//  PTools
//
//  Created by 侯振兴 on 15/8/11.
//  Copyright (c) 2015年 moobox. All rights reserved.
//

#import "CheckPicViewController.h"

@interface CheckPicViewController () {
}

@property (nonatomic, strong) UIImageView *takeImageView;

@end

@implementation CheckPicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    CGFloat imageW = self.checkImage.size.width;
    CGFloat imageH = self.checkImage.size.height;
    
    CGFloat imageViewW,imageViewH;
    
    if ((imageH / imageW) > (kScreenHeight / kScreenWidth)) {
        imageViewH = kScreenHeight;
        imageViewW = imageW * (kScreenHeight / imageH);
    } else {
        imageViewH = imageH * (kScreenWidth / imageW);
        imageViewW = kScreenWidth;
    }
    
    
    self.takeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, imageViewW, imageViewH)];
    _takeImageView.center = CGPointMake(kScreenWidth / 2, (kScreenHeight - 64) / 2);
    _takeImageView.image = _checkImage;
    [self.view addSubview:_takeImageView];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 30, 44);
    
    [backBtn setImage:[UIImage imageNamed:@"back_btn"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;


}
-(void)doBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void) addGestureRecognizerToView:(UIView *)view {
    // 缩放手势
    UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
    [view addGestureRecognizer:pinchGestureRecognizer];
    
    // 移动手势
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
    [view addGestureRecognizer:panGestureRecognizer];
}

// 处理缩放手势
- (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer {
    UIView *view = pinchGestureRecognizer.view;
    if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {
        view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
        if (_takeImageView.frame.size.width < kScreenWidth) {
            _takeImageView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
            //让图片无法缩得比原图小
        }
        if (_takeImageView.frame.size.width > 3 * kScreenWidth) {
            _takeImageView.frame = CGRectMake(-kScreenWidth, -kScreenHeight, 3 * kScreenWidth, 3 * kScreenHeight);
        }
        pinchGestureRecognizer.scale = 1;
    }
}

// 处理拖拉手势
- (void) panView:(UIPanGestureRecognizer *)panGestureRecognizer
{
    UIView *view = panGestureRecognizer.view;
    if (panGestureRecognizer.state == UIGestureRecognizerStateBegan || panGestureRecognizer.state == UIGestureRecognizerStateChanged) {
        CGPoint translation = [panGestureRecognizer translationInView:view.superview];
        [view setCenter:(CGPoint){view.center.x + translation.x, view.center.y + translation.y}];
        [panGestureRecognizer setTranslation:CGPointZero inView:view.superview];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
