gpt4 book ai didi

ios - 是否有同时处理捏合和平移的手势识别器?

转载 作者:可可西里 更新时间:2023-11-01 05:03:28 25 4
gpt4 key购买 nike

因此,我正在使用 iOS 4.2 为我的应用程序添加缩放和平移功能。我已经实现了 UIPinchGestureRecognizer 和 UIPanGestureRecognizer 的实例。在我看来,其中只有一个是一次识别一个手势。特别是,后者仅在一根手指按下时使用react,而前者在第二根手指出现时使用react。这没关系,但我认为它有一些副作用,会导致用户体验质量下降。

当您放下两根手指然后移动其中一根时,图像会像它应该的那样扩展(放大),但手指下方的像素不再位于手指下方。图像从图像中心开始缩放,而不是两根手指之间的中点。而那个中心点本身也在移动。我希望该中心点的移动决定整体图像的平移。

几乎所有 iOS 应用程序都具有这种相同的行为,即图像围绕图像中心放大或缩小,而不是手指下方的像素跟踪手指吗?

在我看来,创建自定义手势识别器是解决此问题的正确设计方法,但在我看来,有人会创建这样的识别器以供商业免费下载和使用。有没有这样的UIGestureRecognizer?

最佳答案

抱歉,很匆忙,但这是我用于我的一个演示应用程序的代码,它可以在不使用 ScrollView 的情况下同时缩放和平移。

不要忘记遵守 UIGestureRecognizerDelegate 协议(protocol)

如果您无法同时获得捏合和平移,可能是因为您缺少此方法:

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

这里是完整的源代码:

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@end

@implementation ViewController

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

isEditing = false;

photoView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[photoView setImage:[UIImage imageNamed:@"photo.png"]];
photoView.hidden = YES;

maskView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[maskView setImage:[UIImage imageNamed:@"maskguide.png"]];
maskView.hidden = YES;

displayImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];

[panGesture setDelegate:self];
[pinchGesture setDelegate:self];

[photoView addGestureRecognizer:panGesture];
[photoView addGestureRecognizer:pinchGesture];
[photoView setUserInteractionEnabled:YES];

[panGesture release];
[pinchGesture release];

btnEdit = [[UIButton alloc] initWithFrame:CGRectMake(60, 400, 200, 50)];
[btnEdit setBackgroundColor:[UIColor blackColor]];
[btnEdit setTitle:@"Start Editing" forState:UIControlStateNormal];
[btnEdit addTarget:self action:@selector(toggleEditing) forControlEvents:UIControlEventTouchUpInside];

[[self view] addSubview:displayImage];
[[self view] addSubview:photoView];
[[self view] addSubview:maskView];
[[self view] addSubview:btnEdit];

[self updateMaskedImage];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

-(void)dealloc
{
[btnEdit release];

[super dealloc];
}

#pragma mark -
#pragma mark Update Masked Image Method
#pragma mark -

-(void)updateMaskedImage
{
maskView.hidden = YES;

UIImage *finalImage =
[self maskImage:[self captureView:self.view]
withMask:[UIImage imageNamed:@"mask.png"]];


maskView.hidden = NO;

//UIImage *finalImage = [self maskImage:photoView.image withMask:[UIImage imageNamed:@"mask.png"]];

[displayImage setImage:finalImage];
}

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {

CGImageRef maskRef = maskImage.CGImage;

CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);

CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
return [UIImage imageWithCGImage:masked];

}

#pragma mark -
#pragma mark Touches Began
#pragma mark -

// adjusts the editing flag to make dragging and drop work
-(void)toggleEditing
{
if(!isEditing)
{
isEditing = true;

NSLog(@"editing...");

[btnEdit setTitle:@"Stop Editing" forState:UIControlStateNormal];

displayImage.hidden = YES;
photoView.hidden = NO;
maskView.hidden = NO;
}
else
{
isEditing = false;

[self updateMaskedImage];

NSLog(@"stopped editting");

[btnEdit setTitle:@"Start Editing" forState:UIControlStateNormal];

displayImage.hidden = NO;
photoView.hidden = YES;
maskView.hidden = YES;
}
}

/*
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if(isEditing)
{
UITouch *finger = [touches anyObject];
CGPoint currentPosition = [finger locationInView:self.view];

//[maskView setCenter:currentPosition];
//[photoView setCenter:currentPosition];
if([touches count] == 1)
{
[photoView setCenter:currentPosition];
}
else if([touches count] == 2)
{

}
}
}
*/

-(void)handlePan:(UIPanGestureRecognizer *)recognizer
{
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}

-(void)handlePinch:(UIPinchGestureRecognizer *)recognizer
{
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
}

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

#pragma mark -
#pragma mark Capture Screen Function
#pragma mark -

- (UIImage*)captureView:(UIView *)yourView
{
UIGraphicsBeginImageContextWithOptions(yourView.bounds.size, yourView.opaque, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[yourView.layer renderInContext:context];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

#pragma mark -

@end

关于ios - 是否有同时处理捏合和平移的手势识别器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11855645/

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