0

我不知道为什么我的缩放、旋转和移动不能同时工作..

我查看了很多示例,但似乎找不到问题所在。首先我认为这是一个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
4

3 回答 3

2

这对我有用:

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
pinchRecognizer.delegate = self;
[self addGestureRecognizer:pinchRecognizer];

UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
pinchRecognizer.delegate = self;
[self addGestureRecognizer:rotationRecognizer];

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
pinchRecognizer.delegate = self;
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[self addGestureRecognizer:panRecognizer];
于 2013-07-18T13:04:19.453 回答
0

好的,你做错的是没有为 UIGestureRecognizer 添加一个对象来适应。例如,您有

 UITapGestureRecognizer *tapProfileImageRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
    [tapProfileImageRecognizer setNumberOfTapsRequired:2];
    [self addGestureRecognizer:tapProfileImageRecognizer];

这正是您所需要的,除了这条线

 [self addGestureRecognizer:tapProfileImageRecognizer];

这是将手势添加到类中。你需要一个对象:)

所以就这样做

 [self.view addGestureRecognizer:tapProfileImageRecognizer];

对于你所有的手势,它应该工作:)你总是必须确保你添加到对象,而不是类

希望这可以帮助!!

编辑:

将 UIGestureRecognizers 放在 UIView 上是您正在做的事情的方式。你现在需要做的是在Xcode下,

  • 文件 -> 新文件
  • 创建一个 UIViewController 子类
  • 确保将其命名为...
  • 在你的 .xib 文件下,找到这个:

在此处输入图像描述

然后,如果您查看 UIView 的位置,请单击它。这将打开 UIView 属性面板。移动到圆圈中有 i 的小按钮。它是“属性”面板下的蓝色。单击它,您应该会看到:

在此处输入图像描述

您在哪里看到显示“UIView”的框

那是你输入你的 UIView 子类的地方。

这应该有助于解决您遇到的任何问题。希望有帮助!:)

于 2013-07-18T11:16:11.867 回答
-1

adding gesture code you should put in viewDidLoad not in init method and add gesture in view(self.view) not in object(self) itself . try as follow I made some changes

-(void) viewDidLoad
{
        UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
        [self.view addGestureRecognizer:pinchRecognizer];

        UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
        [self.view addGestureRecognizer:rotationRecognizer];

        UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
        [panRecognizer setMinimumNumberOfTouches:1];
        [panRecognizer setMaximumNumberOfTouches:1];
        [self.view addGestureRecognizer:panRecognizer];

        UITapGestureRecognizer *tapProfileImageRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
        [tapProfileImageRecognizer setNumberOfTapsRequired:2];
        [self.view addGestureRecognizer:tapProfileImageRecognizer];
}
于 2013-07-18T11:20:12.723 回答