UIGestureRecognizer

#import "HLPanViewController.h"
#import "Masonry.h"

@interface HLPanViewController ()

@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, assign) CGFloat lastScale;

@end

@implementation HLPanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"redPackets_open"]];
    self.imgView.userInteractionEnabled = YES;
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panHandler:)];
    [self.imgView addGestureRecognizer:pan];
    
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationHandler:)];
    [self.view addGestureRecognizer:rotation];
    
    [self.view addSubview:self.imgView];
    
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.width.height.equalTo(@50);
    }];
    
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchHandler:)];
    [self.view addGestureRecognizer:pinch];
}

- (void)panHandler:(UIPanGestureRecognizer *)gesture {
    CGPoint transP = [gesture translationInView:self.imgView];
    NSLog(@"transp x: %f  y: %f", transP.x, transP.y);
    self.imgView.transform = CGAffineTransformTranslate(self.imgView.transform, transP.x, transP.y);
    [gesture setTranslation:CGPointZero inView:self.imgView];
}

- (void)pinchHandler:(UIPinchGestureRecognizer *)gesture {
    switch (gesture.state) {
        case UIGestureRecognizerStateBegan:
            
            break;
        case UIGestureRecognizerStateChanged:
        {
            CGFloat currentScale = [[self.imgView.layer valueForKeyPath:@"transform.scale"] floatValue];
            CGFloat newScale = gesture.scale - self.lastScale + 1;
            
            NSLog(@"currentScale:%f gestureScale:%f lastScale:%f newScale:%f", currentScale, gesture.scale, self.lastScale, newScale);
            newScale = MIN(newScale, 5 / currentScale);
            newScale = MAX(newScale, 1 / currentScale);
            
            self.imgView.transform = CGAffineTransformScale(self.imgView.transform, newScale, newScale);

            self.lastScale = gesture.scale;
        }
            break;
        case UIGestureRecognizerStateEnded:
            self.lastScale = 1;
            break;
        default:
            break;
    }
}

- (void)rotationHandler:(UIRotationGestureRecognizer *)gesture {
        if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
            self.imgView.transform = CGAffineTransformRotate(self.imgView.transform, M_PI/20);
            NSLog(@"%f",gesture.rotation);
            [gesture setRotation:0];
        }
    
        
        //3d旋转效果
//        CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
//        rotationAndPerspectiveTransform.m34 = 1.0 / -500;
//        rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform,gesture.rotation, 0.0f, 1.0f, 0.0f);
//        self.imgView.layer.transform = rotationAndPerspectiveTransform;
}

@end

 

你可能感兴趣的:(iOS)