标签: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