我不知道为什么我的缩放、旋转和移动不能同时工作..
我查看了很多示例,但似乎找不到问题所在。首先我认为这是一个gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer
问题,但不是请帮助:)
这是我的代码:
@implementation StoryEditorPageHolderView
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
- (id)initWithProperty:(Property *)property scale:(CGFloat)scaleFactor pos:(CGPoint)point dustbin:(DustbinView *)dustBin{
self = [super initWithFrame:CGRectZero];
if(self){
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:property.imageName]];
self.frame = CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height);
[self addSubview:_imageView];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[self addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[self addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[self addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapProfileImageRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapProfileImageRecognizer setNumberOfTapsRequired:2];
[self addGestureRecognizer:tapProfileImageRecognizer];
}
return self;
}
- (void)rotate:(UIRotationGestureRecognizer *)rotate {
if (rotate.state == UIGestureRecognizerStateBegan) {
prevRotation = 0.0;
}
float thisRotate = rotate.rotation - prevRotation;
prevRotation = rotate.rotation;
self.transform = CGAffineTransformRotate(self.transform, thisRotate);
}
- (void)scale:(UIPinchGestureRecognizer *)pinch {
if (pinch.state == UIGestureRecognizerStateBegan)
prevPinchScale = 1.0;
float thisScale = 1 + (pinch.scale-prevPinchScale);
prevPinchScale = pinch.scale;
self.transform = CGAffineTransformScale(self.transform, thisScale, thisScale);
}
-(void)move:(UIPanGestureRecognizer *)pan {
if (pan.state == UIGestureRecognizerStateBegan){
prevPanPoint = [pan locationInView:self.superview];
}
CGPoint curr = [pan locationInView:self.superview];
float diffx = curr.x - prevPanPoint.x;
float diffy = curr.y - prevPanPoint.y;
CGPoint centre = self.center;
centre.x += diffx;
centre.y += diffy;
self.center = centre;
prevPanPoint = curr;
}
@end
我在 .h 文件中也有UIGestureRecognizerDelegate
一个:delegate
#import <Foundation/Foundation.h>
@class Property;
@class DustbinView;
@interface StoryEditorPropertyView : UIView <UIGestureRecognizerDelegate>
@property (strong, nonatomic) UIPanGestureRecognizer *panRecognizer;
@property (strong, nonatomic) UIPinchGestureRecognizer *pinchRecognizer;
@property (strong, nonatomic) UIRotationGestureRecognizer *rotationRecognizer;
@property (strong, nonatomic) UITapGestureRecognizer *tapProfileImageRecognizer;
@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) Property *property;
@property (nonatomic) CGPoint pointBegin;
@property (nonatomic) bool isRemoveable;
@property (nonatomic) CGFloat beginScale;
@property (strong, nonatomic) DustbinView *dustBin;
- (id)initWithProperty:(Property *)property scale:(CGFloat)scaleFactor pos:(CGPoint)point dustbin:(DustbinView *)dustBin;
@end