【问题标题】:Keep From Pushing 2 View Controllers at Once避免同时推送 2 个视图控制器
【发布时间】:2014-06-25 02:45:08
【问题描述】:

在我的应用设置中,我有一个带有 4 个 ImageView 的导航控制器。其中 1 个可以拖动,而其他 3 个固定在视图的顶部。使用下面的代码,我进行了设置,以便用户将一个图像视图拖到他想去的图像视图中。所以要进入视图 1,他将可移动的图像视图拖到图像视图 1,依此类推。问题在于,随着图像视图的宽度,选择器视图可能同时触摸两个,这会产生嵌套视图控制器问题。有没有一种方法可以防止这种情况发生,除了将图像视图移动得太远以至于一次不可能选择多个视图?

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    // If the touch was in the placardView, move the placardView to its location
    if ([touch view] == clock) {
        CGPoint location = [touch locationInView:self.tabBarController.view];
        clock.center = location;
        BOOL isIntersecting = CGRectIntersectsRect(clock.frame, prayer.frame);
        BOOL isIntersecting2 = CGRectIntersectsRect(clock.frame, fasting.frame);
        BOOL isIntersecting3 = CGRectIntersectsRect(clock.frame, study.frame);

        if(isIntersecting){
            [self schedulePrayer];
            NSLog(@"prayer");
        }
        if(isIntersecting2){
            [self scheduleFasting];
            NSLog(@"fasting");
        }
        if(isIntersecting3){
            [self scheduleStudying];
            NSLog(@"Studying");
        }
        return;
    }
}

【问题讨论】:

  • 你为什么不直接使用if ... else if ... else if?然后一次只会触发一个。
  • 如果您将此作为答案发布,@Ricky 我会接受。
  • 按要求发布。谢谢。 ;)

标签: ios objective-c uiimageview frame touches


【解决方案1】:

你为什么不直接使用if ... else if ... else if

 if(isIntersecting){
            [self schedulePrayer];
            NSLog(@"prayer");
        }
 else if(isIntersecting2){
            [self scheduleFasting];
            NSLog(@"fasting");
        }
 else if(isIntersecting3){
            [self scheduleStudying];
            NSLog(@"Studying");
        }

那么,一次只会触发一个。

【讨论】:

    【解决方案2】:

    创建另一个 BOOL "isTouching" 并使其全局化。然后在 if(isIntersecting) 中将“isTouching”设置为全局,并添加“isTouching”作为条件:

    if ([touch view] == clock && (!isTouching)) 
    

    如果 UIImageView 不在任何相交视图上,您​​还需要将 isTouching 设置为 false 并且您应该很高兴:)

    这应该足以帮助您解决问题,但如果您需要更多说明,请告诉我。

    【讨论】:

    • 真可惜。因为我刚刚在 XCode 中尝试了确切的场景并且我的解决方案很好。但是由于您已经投反对票,因此我不太愿意进一步解释。对不起
    • 如果你打算花时间回答一个问题,也许回答这个问题而不是说“这应该是足够的提示”之类的话。我没有发布一个希望得到提示的问题。
    • 就像我说的,你的答案不应该包含短语,“这应该是足够的提示”。这会让读者认为你是完美的,无所不知,而且非常居高临下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 2012-06-01
    • 2016-10-22
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多