码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-07 18:11:09      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:property   ted   nonatomic   float   gate   self   turn   tap   bsp   

 

创建一个继承于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];

    }

}

 

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

标签:property   ted   nonatomic   float   gate   self   turn   tap   bsp   

原文地址:http://www.cnblogs.com/hexuejie/p/7490663.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!