我尝试使用手势缩放和旋转 UIImageView。我在互联网上看到了几个示例,并且已经实现了它,但是它们不能一起工作。
- (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 添加到 .h 文件,并将方法添加到 app delegate.m,但当时仍然只有一个手势有效。
@interface ViewController : UIViewController <UIGestureRecognizerDelegate>
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
有人可以帮助我吗?
最佳答案
app delegate.m 的方法?您必须在要添加手势的类中添加该方法,在您的情况下为 viewcontroller.m
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
此方法在 viewcontroller.m 中,不在 appdelegate.m 中
关于objective-c - 使 UIPinchGestureRecognizer 和 UIRotationGestureRecognizer 协同工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13893887/