//
//  ImageCopperView.m
//  PerfectImageCropper
//
//  Created by mac on 16/5/12.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//
//

#import "ImageCropperView.h"
#import <QuartzCore/QuartzCore.h>
#include <math.h>

#import "UIImage+Rotation.h"

@interface ImageCropperView()

@end

@implementation ImageCropperView



- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    if (self) {
       
        [self setup];
        
    }
    
    return self;
}

- (void)setup
{
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor clearColor];
    [self setUserInteractionEnabled:YES];
    [self setMultipleTouchEnabled:YES];
    // 旋转手势
    UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];
    [self addGestureRecognizer:rotationGestureRecognizer];
    
    // 缩放手势
    UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
    [self addGestureRecognizer:pinchGestureRecognizer];
    
    // 移动手势
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
        [panGestureRecognizer setMinimumNumberOfTouches:1];
        [panGestureRecognizer setMaximumNumberOfTouches:1];
    [self addGestureRecognizer:panGestureRecognizer];
////    旋转
//    UIRotationGestureRecognizer *rotateGes = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateImage:)];
//    [self addGestureRecognizer:rotateGes];
//    
////   放大缩小
//    UIPinchGestureRecognizer *scaleGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scaleImage:)];
//    [self addGestureRecognizer:scaleGes];
//
//
////    移动
//    UIPanGestureRecognizer *moveGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveImage:)];
//    [moveGes setMinimumNumberOfTouches:1];
//    [moveGes setMaximumNumberOfTouches:1];
//    [self addGestureRecognizer:moveGes];
//   长按
    UILongPressGestureRecognizer *longPressGR =
    [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress2:)];
    longPressGR.allowableMovement=NO;
    longPressGR.minimumPressDuration = 0.5;
    [self addGestureRecognizer:longPressGR];
   
}
// 处理旋转手势
- (void) rotateView:(UIRotationGestureRecognizer *)rotationGestureRecognizer
{
    UIView *view = rotationGestureRecognizer.view;
    if (rotationGestureRecognizer.state == UIGestureRecognizerStateBegan || rotationGestureRecognizer.state == UIGestureRecognizerStateChanged) {
        view.transform = CGAffineTransformRotate(view.transform, rotationGestureRecognizer.rotation);
        [rotationGestureRecognizer setRotation:0];
    }
}

// 处理缩放手势
- (void) pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer
{
    UIView *view = pinchGestureRecognizer.view;
    if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {
        view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
        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];
    }
}

//float _lastTransX = 0.0, _lastTransY = 0.0;
//- (void)moveImage:(UIPanGestureRecognizer *)sender
//{
//    CGPoint translatedPoint = [sender translationInView:self];
//
//    if([sender state] == UIGestureRecognizerStateBegan) {
//        _lastTransX = 0.0;
//        _lastTransY = 0.0;
//    }
//    
//    CGAffineTransform trans = CGAffineTransformMakeTranslation(translatedPoint.x - _lastTransX, translatedPoint.y - _lastTransY);
//    CGAffineTransform newTransform = CGAffineTransformConcat(self.transform, trans);
//    _lastTransX = translatedPoint.x;
//    _lastTransY = translatedPoint.y;
//    
//    self.transform = newTransform;
//}
//
//float _lastScale = 1.0;
//- (void)scaleImage:(UIPinchGestureRecognizer *)sender
//{
//    if([sender state] == UIGestureRecognizerStateBegan) {
//        
//        _lastScale = 2.0;
//        return;
//    }
//    
//    CGFloat scale = [sender scale]/_lastScale;
//    
//    CGAffineTransform currentTransform = self.transform;
//    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
//    [self setTransform:newTransform];
//    
//    _lastScale = [sender scale];
//}
//
//float _lastRotation = 0.0;
////旋转
//- (void)rotateImage:(UIRotationGestureRecognizer *)sender
//{
//    if([sender state] == UIGestureRecognizerStateEnded) {
//        
//        _lastRotation = 0.0;
//        return;
//    }
//    
//    CGFloat rotation = -_lastRotation + [sender rotation];
//    
//    CGAffineTransform currentTransform = self.transform;
//    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);
//    [self setTransform:newTransform];
//    
//    _lastRotation = [sender rotation];
//    
//}
//长按响应的事件
-(void)handleLongPress2:(id)sender{
    
    [self removeFromSuperview];
    
}


@end
