IOS 手势UIPinchGestureRecognizer(缩放手势) UIRotationGestureRecognizer(旋转手势)

-(void)addPhotos
{
    //像系统相册展示的时候照片之间会有间距
    int xWdith = 20;
    for(int i = 0; i<4; i++)
    {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(xWdith+(xWdith + self.frame.size.width)*i, 0, self.frame.size.width, self.frame.size.height)];
        imgView.backgroundColor = [UIColor orangeColor];
        imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
       
        //缩放手势
        UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]
                                                            initWithTarget:self
                                                            action:@selector(handlePinch:)];
        [imgView addGestureRecognizer:pinchGestureRecognizer];
        imgView.userInteractionEnabled = YES;
        
        //旋转手势
        UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];
        [imgView addGestureRecognizer:rotationGestureRecognizer];
        [photoBrowse addSubview:imgView];
    }
}


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

}

-(void)handlePinch:(UIPinchGestureRecognizer *)pinchGestureRecognizer
{
    UIImageView *view = (UIImageView *)pinchGestureRecognizer.view;
    //    pinchGestureRecognizer.state == UIGestureRecognizerStateChanged
    if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan ) {
        view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
        pinchGestureRecognizer.scale = 2;
    }
}


你可能感兴趣的:(ios开发)