2013-07-18 103 views
0

我不能找出为什么我的缩放,旋转和移动不会在同一时间工作..缩放,旋转与UIGestureRecognizer同时移动

我已经看过很多例子,我可以” t似乎找到了问题。 首先,我认为就是是一个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 

我也有UIGestureRecognizerDelegate在.h文件一个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 

回答

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]; 
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文件,找到这样的:

enter image description here

然后,如果你看看那里UIView的是,单击。这将调出UIView属性面板。旅行到在圈子里有我的小按钮。它是“属性”面板下的蓝色。单击,你应该看到这一点:

enter image description here

你在哪里看到说盒子“的UIView”

这就是你输入你的UIView的子类。

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

+0

它告诉我,: 属性“视图”上的类型 –

+0

的“XXXXXXX”对象没找到你是什么类一个子类? – user2277872

+0

你在做UIViewController或其他任何控制器吗?或者像UIView的子类的东西? – user2277872

-1

添加动作代码,你应该把在viewDidLoadinit方法和视图(self.view)添加手势没有对象(self)本身。 尝试如下我做了一些改变

-(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]; 
} 
+0

这是真的,我没有看到init方法有!哈哈 – user2277872

+0

但是这是属性,将加载视图后在用户选项卡上产生?我不能使用viewDidLoad方法,然后我可以吗? –

+0

添加手势时,他们没有使用UIViewController。它是UIView的子类 – user2277872

相关问题