我正在通过代码制作动态UIViews
并尝试在其上添加UITapGestureRecogniser
。但由于某种原因,他们没有回应。这是代码:
-(void)createRandomBlock{
UIView * block = [[UIView alloc] initWithFrame:CGRectMake([self getRandomPosition], 0-_heightOfBlock, _widthOfBlock, _heightOfBlock)];
block.backgroundColor = [self randomColor];
block.userInteractionEnabled=YES;
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(blockTapped:)];
tapGesture.delegate = self;
tapGesture.numberOfTouchesRequired = 1;
tapGesture.numberOfTapsRequired=1;
[block addGestureRecognizer:tapGesture];
[self.view addSubview:block];
[UIView animateWithDuration:_durationOfAnimation delay:0.0 options:options animations:^{
[block setFrame:CGRectMake(block.frame.origin.x, ScreenHeight, block.bounds.size.width, block.bounds.size.height)];
} completion:^(BOOL finished) {
[block removeFromSuperview];
}];
}
-(void)blockTapped:(UITapGestureRecognizer*)gesture{
NSLog(@"I am being called?");
UIView * block = (UIView*)[gesture view];
[block removeFromSuperview];
}
有人可以帮忙吗?
由于
答案 0 :(得分:1)
你可以在框架上犯错误。请更改框架组&你可以解决你的问题。
-(void)createRandomBlock{
int x_coord = arc4random() % 200; //Random number from 0-320 // set random x position
int y_coord = arc4random() % 481; //Random number from 0-480 // set random y position
UIView * block = [[UIView alloc] initWithFrame:CGRectMake(x_coord, y_coord, 100, 100)]; // for testing set width & height 100 * 100
block.backgroundColor = [UIColor redColor]; // for fix color you can use your change color function
block.userInteractionEnabled=YES;
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(blockTapped:)];
tapGesture.delegate = self;
tapGesture.numberOfTouchesRequired = 1;
tapGesture.numberOfTapsRequired=1;
[block addGestureRecognizer:tapGesture];
[self.view addSubview:block];
[UIView animateKeyframesWithDuration:1.0 delay:0.0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
[block setFrame:CGRectMake(block.frame.origin.x, 0, block.bounds.size.width, block.bounds.size.height)];
} completion:^(BOOL finished) {
// [block removeFromSuperview];
}];
}
-(void)blockTapped:(UITapGestureRecognizer*)gesture{
NSLog(@"I am being called?");
UIView * block = (UIView*)[gesture view];
[block removeFromSuperview];
}
答案 1 :(得分:1)
我尝试使用相同的代码,它对我有用。 仅更改内容,我没有为tapGesture 设置委托。
-(void)createRandomBlock{
UIView * block = [[UIView alloc] initWithFrame:randomPosition];
block.backgroundColor = [self randomColor];
block.userInteractionEnabled=YES;
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(blockTapped:)];
tapGesture.numberOfTapsRequired=1;
[block addGestureRecognizer:tapGesture];
[self.view addSubview:block];
}
-(void)blockTapped:(UITapGestureRecognizer*)gesture{
NSLog(@"I am being called?");
UIView * block = (UIView*)[gesture view];
[block removeFromSuperview];
}
我想因为添加了委托,它会覆盖你指定的选择器,而是调用委托方法。
答案 2 :(得分:1)
如果您的顶级视图的用户互动已停用,则您的手势也无法识别子视图上的互动。尝试在调用createRandomBlock消息之前在某处添加休息行。
self.view.userInteractionEnabled = YES;
答案 3 :(得分:1)
我不知道你的[self getRandomPosition]和_widthOfBlock等有什么价值,否则在我的演示中代码工作正常。
-(void)createRandomBlock{
UIView * block = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
block.backgroundColor = [UIColor redColor];
block.userInteractionEnabled=YES;
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(blockTapped:)];
tapGesture.delegate = self;
tapGesture.numberOfTouchesRequired = 1;
tapGesture.numberOfTapsRequired=1;
[block addGestureRecognizer:tapGesture];
[self.view addSubview:block];
}
-(void)blockTapped:(UITapGestureRecognizer*)gesture{
NSLog(@"I am being called?");
UIView * block = (UIView*)[gesture view];
[block removeFromSuperview];
}
更新:
您应该使用touchesBegan
来识别触摸,这是工作代码,
-(void)createRandomBlock{
UIView * block = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
block.backgroundColor = [UIColor redColor];
block.userInteractionEnabled=YES;
block.tag = 100;
self.view.userInteractionEnabled =YES;
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(blockTapped:)];
// tapGesture.delegate = self;
// tapGesture.numberOfTouchesRequired = 1;
tapGesture.numberOfTapsRequired=1;
[block addGestureRecognizer:tapGesture];
[self.view addSubview:block];
[UIView animateWithDuration:5.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
[block setFrame:CGRectMake(block.frame.origin.x, self.view.frame.size.height-200, block.bounds.size.width, block.bounds.size.height)];
} completion:^(BOOL finished) {
[block removeFromSuperview];
}];
}
-(void)blockTapped:(UITapGestureRecognizer*)gesture{
NSLog(@"I am being called?");
UIView * block = (UIView*)[gesture view];
[block removeFromSuperview];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
UIView *tempView = [self.view viewWithTag:100];
if ([tempView.layer.presentationLayer hitTest:touchLocation]) {
NSLog(@"call");
[tempView removeFromSuperview];
}
}
答案 4 :(得分:0)
您正在删除完成块中的视图,这意味着在完成动画后,您的视图将被删除。从完成块中删除以下行。
[block removeFromSuperview];
你不应该这样做。您的视图本身将被删除Tap Gesture的工作方式。