我嘗試使用手勢縮放和旋轉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;
}
有人可以幫我嗎?
它始終是一樣的識別器嗎?你有沒有檢查他們兩個單獨工作?即只添加它們。 –
如果我將開始捏,那麼它會縮放圖像,但我不能捏和旋轉在同一時間。如果我將開始旋轉,然後嘗試在同一時間捏,然後再次只有旋轉的作品。 – repoguy