给图片添加缩放旋转放大手势

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了给图片添加缩放旋转放大手势相关的知识,希望对你有一定的参考价值。

 

创建一个继承于uiimageview的子试图 在头文件中添加手势

@interface LXQuaereEditImageView : LXBaseImageView <UIGestureRecognizerDelegate>

@property (nonatomic,strong,readonly) UIPinchGestureRecognizer *pinchGestureRecognizer;

@property (nonatomic,strong,readonly) UIPanGestureRecognizer *panGestureRecognizer;

@property (nonatomic,strong,readonly) UIRotationGestureRecognizer *rotationGestureRecognizer;

@end

 

在初始化的地方初始化手势

_tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureDetected:)];

    [_tapGestureRecognizer setDelegate:self];

    

    _pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureDetected:)];

    [_pinchGestureRecognizer setDelegate:self];

    

    _panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureDetected:)];

    [_panGestureRecognizer setDelegate:self];

    

    _rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureDetected:)];

    [_rotationGestureRecognizer setDelegate:self];

    

    [self addGestureRecognizer:_pinchGestureRecognizer];

    [self addGestureRecognizer:_panGestureRecognizer];

    [self addGestureRecognizer:_rotationGestureRecognizer];

设置该试图的交互性

    [self setUserInteractionEnabled:YES];

 

设置代理

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

    return YES;

}

 

 

手势触发事件 

- (void)pinchGestureDetected:(UIPinchGestureRecognizer *)recognizer

{

    UIGestureRecognizerState state = [recognizer state];

    

    if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)

    {

        CGFloat scale = [recognizer scale];

        [recognizer.view setTransform:CGAffineTransformScale(recognizer.view.transform, scale, scale)];

        [recognizer setScale:1.0];

    }

    if ([self.delegate respondsToSelector:@selector(editImageViewPinchGesture:)]){

        [self.delegate editImageViewPinchGesture:self];

    }

}

 

- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer

{

    UIGestureRecognizerState state = [recognizer state];

    

    if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)

    {

        CGPoint translation = [recognizer translationInView:recognizer.view];

        [recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)];

        [recognizer setTranslation:CGPointZero inView:recognizer.view];

    }

    if ([self.delegate respondsToSelector:@selector(editImageViewPanGesture:)]){

        [self.delegate editImageViewPanGesture:self];

    }

}

 

- (void)rotationGestureDetected:(UIRotationGestureRecognizer *)recognizer

{

    UIGestureRecognizerState state = [recognizer state];

    

    if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)

    {

        CGFloat rotation = [recognizer rotation];

        [recognizer.view setTransform:CGAffineTransformRotate(recognizer.view.transform, rotation)];

        [recognizer setRotation:0];

    }

    

    if ([self.delegate respondsToSelector:@selector(editImageViewRotationGesture:)]){

        [self.delegate editImageViewRotationGesture:self];

    }

}

 

以上是关于给图片添加缩放旋转放大手势的主要内容,如果未能解决你的问题,请参考以下文章

android如何对viewpager里面的图片进行缩放

js实现移动端图片预览:手势缩放, 手势拖动,双击放大...

vue 快速给图片添加,点击旋转、放大、缩小、拖动的效果

4.页面查看图片时控制图片的旋转放大和缩放(修订版)

同时检测捏合和旋转手势

Unity 3D 物体旋转放大。