2012-12-15 65 views
1

我嘗試使用手勢縮放和旋轉UIImageView。我在互聯網上看到過幾個例子,我已經實現了它,但它不能一起工作。使UIPinchGestureRecognizer和UIRotationGestureRecognizer一起工作

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)]; 
    // imgView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height); 
    imgView.image = [UIImage imageNamed:@"spotItInLondonIcon.png"]; 
    [imgView setContentMode:UIViewContentModeScaleAspectFit]; 
    [imgView setMultipleTouchEnabled:YES]; 
    [imgView setUserInteractionEnabled:YES]; 
    [self.view addSubview:imgView]; 

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

    UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)]; 
    [imgView addGestureRecognizer:rotate]; 
    rotate.delegate = self; 
} 
-(void)pinch: (UIPinchGestureRecognizer*)sender 
{ 
    CGFloat scale = sender.scale; 
    imgView.transform = CGAffineTransformScale(imgView.transform, scale, scale); 
    sender.scale = 1.0; 
    NSLog(@"pinch executed"); 
} 
-(void)rotation: (UIRotationGestureRecognizer*)rotationDetected 
{ 
    CGFloat angle = rotationDetected.rotation; 
    imgView.transform = CGAffineTransformRotate(imgView.transform, angle); 
    rotationDetected.rotation = 0.0; 
    NSLog(@"rotation executed"); 
} 

然後我將委託添加到應用程序delegate.m的.h文件和方法,但仍然只有一個手勢在工作。

@interface ViewController : UIViewController <UIGestureRecognizerDelegate> 

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

有人可以幫我嗎?

+0

它始終是一樣的識別器嗎?你有沒有檢查他們兩個單獨工作?即只添加它們。 –

+0

如果我將開始捏,那麼它會縮放圖像,但我不能捏和旋轉在同一時間。如果我將開始旋轉,然後嘗試在同一時間捏,然後再次只有旋轉的作品。 – repoguy

回答

3

方法應用delegate.m ??你必須添加在您添加的姿勢,你的情況的類中的方法,viewcontroller.m

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

這種方法viewcontroller.m不在的appdelegate。 m

+0

這就是解決方案!我不知道,該方法看起來應該在appDelegate中,但現在我知道我是我的壞人。謝謝你的答案。 – repoguy

+0

很高興它爲你工作好運:) –

相關問題