【发布时间】:2016-04-18 12:00:32
【问题描述】:
我正在通过代码制作动态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];
}
有人可以帮忙吗?
谢谢
【问题讨论】:
-
createRandomBlock 是否被调用过一次?
-
大概你可以看到屏幕上的视图?视图和超级视图上还有哪些其他手势?
-
createRandomBlock 正在使用延迟为 3 秒的计时器调用。另外,我正在使用 uiviewanimate 为视图设置动画。
-
是的,我可以在那里看到 UIView。我还可以看到它是动画的。
-
代码已更新。我认为我的 UIViewAnimate 导致了这个问题。
标签: ios objective-c uitapgesturerecognizer