iOS开发之实现点击图片进行缩放,旋转等功能

因为项目需求要实现将图片  1.捏取缩放 2.点击缩放3.旋转 4.随意拖拽 5.单击返回图片原来大小等
直接上码:

第一步:

@interface FirmwareViewController () 

@property (strong, nonatomic)  UIImageView *tipImage;
@property (nonatomic) int flag;

@implementation FirmwareViewController
{
    CGRect oldFrame;    //保存图片原来的大小
    CGRect largeFrame;  //确定图片放大最大的程度
}

第二步:

- (void)viewDidLoad {
    [super viewDidLoad];
    _flag =1;
    
    // 单击
    UITapGestureRecognizer *SingleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resetImage:)];
    SingleTapGesture.numberOfTapsRequired = 1;//tap次数
    [self.view addGestureRecognizer:SingleTapGesture];
    // 双击
    UITapGestureRecognizer *doubleTapGesture;
    doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTapFrom)];
    doubleTapGesture.numberOfTapsRequired = 2;
    [self.view addGestureRecognizer:doubleTapGesture];
    // 关键在这一行,如果双击确定偵測失败才會触发单击
    [SingleTapGesture requireGestureRecognizerToFail:doubleTapGesture];
    
    
    [self addGestureRecognizerToView:_tipImage];
    //如果处理的是图片,别忘了
    [_tipImage setUserInteractionEnabled:YES];
    [_tipImage setMultipleTouchEnabled:YES];

    oldFrame = _tipImage.frame;
    largeFrame = CGRectMake(ScreenWidth,ScreenHeight, 3 * oldFrame.size.width, 3 * oldFrame.size.height);
    [self addGestureRecognizerToView:_tipImage];
    [self.view addSubview:_tipImage];
    _tipImage.userInteractionEnabled = YES;

}

第三步:

//单击实现原来大小
- (void)resetImage:(UITapGestureRecognizer *)recognizer
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    // 恒等变换. beginAnimation 和 commitAnimation 间的操作为动画过程
    _tipImage.transform = CGAffineTransformIdentity;
    [_tipImage setCenter:CGPointMake(self.view.frame.size.height/3.5, self.view.frame.size.width/1.23)];
    [UIView commitAnimations];
}

//双击实现放大和缩小一倍
- (void) handleDoubleTapFrom {
    if (_flag == 1) {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];
        
        [_tipImage setFrame:CGRectMake(_tipImage.frame.origin.x -_tipImage.frame.size.width / 2,_tipImage.frame.origin.y - _tipImage.frame.size.height / 2,2 * _tipImage.frame.size.width,2 * _tipImage.frame.size.height)];
        [UIView commitAnimations];
        _flag = 0;
    }
    else {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];
        [_tipImage setFrame:CGRectMake(_tipImage.frame.origin.x+_tipImage.frame.size.width/4, _tipImage.frame.origin.y + _tipImage.frame.size.height/4, _tipImage.frame.size.width/2, _tipImage.frame.size.height/2)];
        [UIView commitAnimations];
        _flag = 1;
    }
}

第四步:

#pragma mark - 实现点击图片缩放,移动等功能
// 添加所有的手势
- (void) addGestureRecognizerToView:(UIView *)view
{
    // 旋转手势
    UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];
    [view addGestureRecognizer:rotationGestureRecognizer];
    
    // 缩放手势
    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) 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];
    }
}

 

你可能感兴趣的:(iOS)