UIImageView子类不能同时工作

时间:2012-10-24 13:59:27

标签: objective-c ios ipad uiimageview subclass

我正在开发适用于iPad的iOs 6应用程序。我开发了一个UIImageView子类,添加了手势识别(缩放,捏和平移),但我遇到了一个问题:它不会同时承认手势。我首先将它编码为普通的UIImageView,它工作得很好,但现在它的子类却没有。我的代码:

image.m(UIImageView子类)

- (id)initWithImage:(UIImage *)image {
self = [super initWithImage:image];

if (self) {

    [self setMultipleTouchEnabled:YES];
    [self setUserInteractionEnabled:YES];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDetected:)];
    [super addGestureRecognizer:panRecognizer];

    UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchDetected:)];
    [super addGestureRecognizer:pinchRecognizer];

    UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationDetected:)];
    [super addGestureRecognizer:rotationRecognizer];

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
    tapRecognizer.numberOfTapsRequired = 2;
    [super addGestureRecognizer:tapRecognizer];

    NSLog(@"lkjlkj");
}
return self;}

- (void)panDetected:(UIPanGestureRecognizer *)panRecognizer {
CGPoint translation = [panRecognizer translationInView:self.superview];
CGPoint imageViewPosition = self.center;
imageViewPosition.x += translation.x;
imageViewPosition.y += translation.y;

self.center = imageViewPosition;
[panRecognizer setTranslation:CGPointZero inView: self.superview];}

  - (void)pinchDetected:(UIPinchGestureRecognizer *)pinchRecognizer{    

CGFloat scale = pinchRecognizer.scale;
self.transform = CGAffineTransformScale(self.transform, scale, scale);
pinchRecognizer.scale = 1.0;}

- (void)rotationDetected:(UIRotationGestureRecognizer *)rotationRecognizer{
CGFloat angle = rotationRecognizer.rotation;
self.transform = CGAffineTransformRotate(self.transform, angle);
rotationRecognizer.rotation = 0.0;}

- (void)tapDetected:(UITapGestureRecognizer *)tapRecognizer{
[UIView animateWithDuration:0.25 animations:^{
    self.center = CGPointMake(CGRectGetMidX(self.superview.bounds), CGRectGetMidY(self.superview.bounds));
    self.transform = CGAffineTransformIdentity;
}];}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;}

viewcontroller.m(我从图像中创建一个不同时响应多个手势的图像视图)

image *img = [[image alloc] initWithImage:imatgetemporal];    
img.center = CGPointMake(525/2, 651/2);


[[self.view viewWithTag:2] addSubview:img];

谢谢大家!!

1 个答案:

答案 0 :(得分:5)

我想您可以通过在声明它们之后向UIPanGestureRecognizerUIPinchGestureRecognizerUIRotationGestureRecognizerUITapGestureRecognizer添加相同的委托来解决此问题。代码如下:

tapRecognizer.delegate = self;
pinchRecognizer.delegate = self;
rotationRecognizer.delegate = self;
tapRecognizer.delegate = self;

希望它有所帮助。