2012-10-24 40 views
2

我正在爲iPad開發iOs 6應用程序。我開發了一個UIImageView子類,添加手勢識別(縮放,捏和平移),但是我遇到了一個問題:它不允許同時使用手勢。我首先將它編碼爲一個普通的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]; 

謝謝(我來自哪裏圖像不同時多手勢響應創建的ImageView)對全部!!

回答

5

我想你可以通過添加相同的委託到UIPanGestureRecognizerUIPinchGestureRecognizerUIRotationGestureRecognizer並向UITapGestureRecognizer剛剛宣佈他們以後解決。代碼將是這樣的:

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

希望它有幫助。

相關問題