Xcode 使用UIGestureRecognitor方法中的单键更改动态UIView子视图
更新:问题已解决,请参见下文 情况:我在nib中的UIScrollView上有几个动态加载的UIView 预期行为:我想点击任意一个UIView,它会改变背景颜色以指示它被点击。如果它已经被点击,那么它应该会变回最初的样子 我已经在每个UIView上设置了一个UICapsive识别器,下面是我执行行为的选择器方法。我把自己弄糊涂了。我为这里的粗略逻辑道歉(这是一个拉夫草案)。我已经在文件的init中设置了一个设置为“NO”的isTapped BOOLXcode 使用UIGestureRecognitor方法中的单键更改动态UIView子视图,xcode,uiview,tap,Xcode,Uiview,Tap,更新:问题已解决,请参见下文 情况:我在nib中的UIScrollView上有几个动态加载的UIView 预期行为:我想点击任意一个UIView,它会改变背景颜色以指示它被点击。如果它已经被点击,那么它应该会变回最初的样子 我已经在每个UIView上设置了一个UICapsive识别器,下面是我执行行为的选择器方法。我把自己弄糊涂了。我为这里的粗略逻辑道歉(这是一个拉夫草案)。我已经在文件的init中设置了一个设置为“NO”的isTapped BOOL - (void)handleSingleTa
- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {
isTapped = !isTapped;
UIView *v = gestureRecognizer.view;
NSInteger currentIndex = [studentCellArray indexOfObjectIdenticalTo:v];
if (oldIndex != currentIndex) {
isTapped = YES;
}
//check to see if obj in array then switch on/off
if ([tappedViewArray indexOfObjectIdenticalTo:v] != NSNotFound) {
oldIndex = currentIndex;
}
if (currentIndex == v.tag) {
isTapped = !isTapped;
}
if (isTapped) {
[tappedViewArray addObject:v];
[super formatViewTouchedNiceGrey:v];
}else{
[tappedViewArray removeObject:v];
[super formatViewBorder:v];
}
if (currentIndex == oldIndex) {
isTapped = !isTapped;
}
}
实际行为:点击第一个UIView后,它选择fine并更改,第二次点击将更改回UIView,但在连续点击后,它仍保持选中状态。此外,如果选择一个UIView并转到另一个视图,则必须双击连续视图
我只想点击一次以关闭或打开scrollview中的任何UIView
更新:好吧,在一些手写和其他徒劳的尝试之后,我试图专注于这个问题——我已经用这种方式解决了它,它表现得很好
以下是我的解决方案:
- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {
isTapped = !isTapped;
UIView *v = gestureRecognizer.view;
NSInteger currentIndex = [studentCellArray indexOfObjectIdenticalTo:v];
if (((isTapped && currentIndex != oldIndex) || (!isTapped && currentIndex != oldIndex)) && [tappedViewArray indexOfObject:v] == NSNotFound) {
oldIndex = currentIndex;
[tappedViewArray addObject:v];
[super formatCornerRadiusWithGreyBackgrnd:v];
} else {
[super formatViewBorder:v];
[tappedViewArray removeObject:v];
}
}
所以我希望这能帮助一些人解决这个问题
关键是要检查isTapped和索引是否不相等,以及视图对象是否不在我正在组装的数组中,以指示触摸/点击的项目