在UIGestureRecognizer方法中使用单击更改动态UIView子视图

时间:2012-01-11 15:15:20

标签: xcode uiview tap

更新:解决了问题,见下文!

情况:我在笔尖的UIScrollView上有几个动态加载的UIViews。

预期的行为:我想单独使用TAP中的任何一个UIViews,它会改变背景颜色以指示它被点击。如果它已经被轻拍,那么它应该改回原来的样子。

我已经在每个UIViews上设置了一个UITapGesture识别器,这里是我正在做行为的选择器方法。我弄糊涂了自己。我为这里的粗略逻辑道歉(这是一个粗略的草案)。我最初在文件的init中设置了一个isTapped BOOL设置为“NO”。

- (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后,它选择罚款并更改,第二个点击将更改它,但是在连续点击后它保持选中状态。此外,如果您选择UIView并转到另一个视图 - 您必须双击连续视图。

我想点击一次关闭或关闭滚动视图中的任何UIViews。

更新:嗯,经过一些手写和试图专注于这个问题的其他徒劳的尝试----我已经用这种方式解决了它并且它正确行事!

这是我的解决方案:

- (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和索引是否不相等并且视图对象不在我正在组装的数组中以指示触摸的项目/ Tapped ....

0 个答案:

没有答案