1

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

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

预期行为:我想单独点击任何一个 UIView,它会更改背景颜色以指示它已被点击。如果它已经被点击,那么它应该变回它的初始外观。

我已经在每个 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 并转到另一个视图 - 您必须双击连续的视图。

我只想点击一次以关闭或打开滚动视图中的任何 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 和索引是否不相等以及视图对象不在我正在组装的数组中以指示已触摸/已点击的项目....

4

0 回答 0