gpt4 book ai didi

iphone - 如何同时检测主视图 UILongPressGestureRecognizer 和 subview UIPanGestureRecognizer

转载 作者:行者123 更新时间:2023-11-29 11:12:34 32 4
gpt4 key购买 nike

我的 UILongPressGestureRecognizer 和 UIPanGestureRecognizer 遇到了问题。

我希望在我的 self.view 中长按以在我的 self.view 中添加一个带有 UIPanGestureRecognizer 的 View 。

但是 UIPanGestureRecognizer 无法识别。

我错过了什么吗?

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

UILongPressGestureRecognizer *tapRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(addImgView:)];
tapRecognizer.numberOfTouchesRequired = 1;
tapRecognizer.minimumPressDuration = 0.7;
[tapRecognizer setDelegate:self];
self.view.userInteractionEnabled = YES;

[self.view addGestureRecognizer:tapRecognizer];
}

-(void)addImgView:(UILongPressGestureRecognizer *)recognizer
{
NSLog(@"tappppp");

if(UIGestureRecognizerStateBegan == recognizer.state) {
// Called on start of gesture, do work here

NSLog(@"UIGestureRecognizerStateBegan");
UIImage *img = [UIImage imageNamed:@"beer.png"];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];

UILabel *timeStamp = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
timeStamp.text = [NSString stringWithFormat:@"%f",[NSDate date]];

UIView *drinkView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
[drinkView setUserInteractionEnabled:YES];

CGPoint tapLocation = [recognizer locationInView:recognizer.view];

[timeStamp setCenter:CGPointMake(tapLocation.x, tapLocation.y)];
[imgView setCenter:CGPointMake(tapLocation.x,tapLocation.y)];

[drinkView addSubview:imgView];
[drinkView addSubview:timeStamp];

[drinkView setUserInteractionEnabled:YES];

[imgView setUserInteractionEnabled:YES];
[timeStamp setUserInteractionEnabled:YES];

UIPanGestureRecognizer *recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan1:)];
[recognizer1 setDelegate:self];
[drinkView addGestureRecognizer:recognizer1];
[self.view addSubview:drinkView];

}

}

最佳答案

-(void)addImgView:(UILongPressGestureRecognizer *)recognizer
{
NSLog(@"tappppp");

if(UIGestureRecognizerStateBegan == recognizer.state) {
// Called on start of gesture, do work here

NSLog(@"UIGestureRecognizerStateBegan");
UIImage *img = [UIImage imageNamed:@"beer.png"];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];

UILabel *timeStamp = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 30)];
timeStamp.text = [NSString stringWithFormat:@"%f",[NSDate date]];

UIView *drinkView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 75,115)];
drinkView.backgroundColor=[UIColor redColor];
[drinkView setUserInteractionEnabled:YES];

CGPoint tapLocation = [recognizer locationInView:recognizer.view];

[drinkView setCenter:CGPointMake(tapLocation.x,tapLocation.y)];



[drinkView setUserInteractionEnabled:YES];
[imgView setUserInteractionEnabled:YES];
[timeStamp setUserInteractionEnabled:YES];

[drinkView addSubview:imgView];
[drinkView addSubview:timeStamp];


UIPanGestureRecognizer *recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan1:)];
[recognizer1 setDelegate:self];
[drinkView addGestureRecognizer:recognizer1];


[self.view addSubview:drinkView];

}

if(UIGestureRecognizerStateChanged == recognizer.state) {
// Do repeated work here (repeats continuously) while finger is down
NSLog(@"UIGestureRecognizerStateChanged");

}

if(UIGestureRecognizerStateEnded == recognizer.state) {
// Do end work here when finger is lifted
NSLog(@"UIGestureRecognizerStateEnded");

}
}

关于iphone - 如何同时检测主视图 UILongPressGestureRecognizer 和 subview UIPanGestureRecognizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10890246/

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