在 else if 条件中迭代变量?

发布于 2024-12-10 04:53:08 字数 1390 浏览 0 评论 0原文

我正在尝试创建一个 if 语句,其条件取决于变量对象。我该怎么办?

这是我想做的事情的要点...

if ([gestureRecognizer state] == UIGestureRecognizerStateEnded)
{       
    if (somecondition)
    {
        //Do stuff
    }
    else if (CGRectIntersectsRect(currentImage.frame, **anyOfTheImagesInMyArray**.frame))
    {
        //Do stuff
    }   
}

如何在测试 true 或 false 之前让 else if 条件检查多个 UIView 框架?

更新的代码:

在这种情况下我该怎么办?

if ([gestureRecognizer state] == UIGestureRecognizerStateEnded)
{       
    if (CGRectIntersectsRect(currentImage.frame, **anyOfTheImagesInMyArray_1**.frame))
    {
        //Do stuff
    }
    else if (CGRectIntersectsRect(currentImage.frame, **anyOfTheImagesInMyArray_2**.frame))
    {
        if (condition) {
            //do stuff
        }
        else if (condition){
            //do something else
        }
    }   
    else if (CGRectIntersectsRect(currentImage.frame, **anyOfTheImagesInMyArray_3**.frame))
    {
        if (condition) {
            //do stuff
        }
        else if (condition){
            //do something else
        }
    }   
    else if (CGRectIntersectsRect(currentImage.frame, **anyOfTheImagesInMyArray_4**.frame))
    {
        if (condition) {
            //do stuff
        }
        else if (condition){
            //do something else
        }
    }   
}

I'm trying to create an if statement with the condition depending on a variable object. How can I go about this?

Here is the gist of what I'm trying to do...

if ([gestureRecognizer state] == UIGestureRecognizerStateEnded)
{       
    if (somecondition)
    {
        //Do stuff
    }
    else if (CGRectIntersectsRect(currentImage.frame, **anyOfTheImagesInMyArray**.frame))
    {
        //Do stuff
    }   
}

How can I get the else if condition to check more than one UIView frame before testing true or false?

Updated Code:

What do I do in this case?

if ([gestureRecognizer state] == UIGestureRecognizerStateEnded)
{       
    if (CGRectIntersectsRect(currentImage.frame, **anyOfTheImagesInMyArray_1**.frame))
    {
        //Do stuff
    }
    else if (CGRectIntersectsRect(currentImage.frame, **anyOfTheImagesInMyArray_2**.frame))
    {
        if (condition) {
            //do stuff
        }
        else if (condition){
            //do something else
        }
    }   
    else if (CGRectIntersectsRect(currentImage.frame, **anyOfTheImagesInMyArray_3**.frame))
    {
        if (condition) {
            //do stuff
        }
        else if (condition){
            //do something else
        }
    }   
    else if (CGRectIntersectsRect(currentImage.frame, **anyOfTheImagesInMyArray_4**.frame))
    {
        if (condition) {
            //do stuff
        }
        else if (condition){
            //do something else
        }
    }   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅我要高飞 2024-12-17 04:53:08

不确定我的问题是否正确,但如果您想对数组中的每个图像执行一些任务,它应该如下所示:

if ([gestureRecognizer state] == UIGestureRecognizerStateEnded)
{
    // imagesArray is an NSArray holding UIImages
    for (UIImage *image in imagesArray) {
        if (CGRectIntersectsRect(currentImage.frame, image.frame)) {
            if (someCondition) {
                // do stuff
            } else {
                // do something else
            }
        }
    }
}

Not sure if I got the question right, but if you want to perform some tasks on every image in the array it should look e.g. like this:

if ([gestureRecognizer state] == UIGestureRecognizerStateEnded)
{
    // imagesArray is an NSArray holding UIImages
    for (UIImage *image in imagesArray) {
        if (CGRectIntersectsRect(currentImage.frame, image.frame)) {
            if (someCondition) {
                // do stuff
            } else {
                // do something else
            }
        }
    }
}
命硬 2024-12-17 04:53:08

据我所知,没有办法简洁地使用现有语法来完成您想要做的事情。相反,请考虑对 NSArray 进行分类,添加一个方法,例如 -containsIntersectingRect:(CGRect)rect,该方法循环遍历 NSValue 对象数组 - 数组只能包含对象,因此您必须将 CGRect 插入到包装为 NSValue 对象的数组中,并查找至少一种肯定的情况CGRectIntersectsRect

这至少会使语法更加集中并抽象为用于一系列条件的单个方法。

To my knowledge, there is no way to do what you're trying to do with existing syntax succinctly. Instead, consider categorizing NSArrayto add a method such as -containsIntersectingRect:(CGRect)rect, which loops through the array of NSValue objects—arrays can only contain objects, so you will have to insert your CGRects into the array wrapped as NSValue objects—and looks for at least one positive case for CGRectIntersectsRect.

This will at least make the syntax more collected and abstracted into a single method for your series of conditionals.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文