gpt4 book ai didi

objective-c - 使 UIPinchGestureRecognizer 和 UIRotationGestureRecognizer 一起工作

转载 作者:行者123 更新时间:2023-12-04 05:18:58 25 4
gpt4 key购买 nike

我尝试使用手势缩放和旋转 UIImageView。我在 Internet 上看到了几个示例并且我已经实现了它,但是它不能一起工作。

- (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;
}

有人能帮我吗?

最佳答案

应用程序 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com