UITapGestureRecognizer 仅适用于初始化时在舞台上可见的项目 [英] UITapGestureRecognizer only working for items visible on stage at initialization
问题描述
因此,我遍历一个循环并创建包含 UIImageView
的 UIView
s(以便我可以有选择地显示任何给定部分).这些UIView
都存储在一个UIScrollView
中.
So, I iterate through a loop and create UIView
s which contain UIImageView
s (So that I can selectively show any given part). These UIView
s are all stored in a UIScrollView
.
我将手势识别器添加到创建它们的循环中的 UIView
中.
I add gesture recognizers to the UIView
s in the loop where I created them.
当我运行程序时,只有 UIScrollView
中最初可见的项目才能识别其手势.如果我滚动到以前隐藏的项目然后点击它们,则根本没有任何反应(从未识别或尝试识别手势).
When I run the program, only the items initially visible within the UIScrollView
have their gestures recognized. If I scroll over to previously hidden items and then tap on them, nothing happens at all (the gesture is never recognized or attempted to be).
初始化代码:
UITapGestureRecognizer* gestRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
gestRec.delegate = self;
[imageholder addGestureRecognizer:gestRec];
处理手势的代码:
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
float count = [self._imageHolders count];
NSLog(@"handling gesture: %f",count);
while(count--){
UIView* object = (UIView*) [self._imageHolders objectAtIndex:count];
// NSLog(@"Whats going on: %@, %@, %b",object,gestureRecognizer.view, object == gestureRecognizer.view);
if(object == gestureRecognizer.view){
object.alpha = .1;
count = 0;
}
// [object release];
}
}
有什么想法吗?
---- 更新:
我探索了 scrollview
、UIView
和手势识别器中的各种可用功能,并尝试弄乱边界,以防某些东西被那样切断... 有趣的是,如果有一个项目只有部分可见,而您将其移过以使其完全可见,则只有最初可见的部分会识别任何手势.
I've explored a variety of the available functions in scrollview
, UIView
and the gesture recognizer and have tried messing with the bounds in case something was cut off that way... Interestingly enough, if there is one item only partially visible and you move it over so it's completely visible, only the portion originally visible will recognize any gestures.
我对手势识别器如何在 UIKit
架构中工作的了解不够,无法理解这个问题.带有手势的滚动视图的 Apple 示例似乎没有这个问题,但我找不到任何真正的区别,除了我将 UIImageViews
嵌套在它们自己的 UIViews中代码>
I don't know enough about how the gesture recognizer works within the UIKit
architecture to understand this problem. The Apple example for a scrollview with gestures doesn't seem to have this problem, but I can't find any real differences, except that I am nesting my UIImageViews
within their own UIViews
推荐答案
我有一个类似的问题,发现它是由于将子视图添加到顶级视图,然后将该顶级视图添加到滚动 -看法.顶级视图的大小必须与滚动视图的 contentSize(不是边界)相同,否则即使子视图滚动到视图中,它也不会将触摸事件传递给它的子视图.
I had a similar problem and found it was caused by adding the sub-views to a top-level view then adding that top-level view to the scroll-view. The top-level view had to be sized to the same dimensions as the contentSize (not the bounds) of the scroll-view otherwise it wouldn't pass on touch events to its subviews even when they had scrolled into view.
这篇关于UITapGestureRecognizer 仅适用于初始化时在舞台上可见的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!