【问题标题】:CGRectIntersectsRect Logic - Is there a better/easier way to accomplish what I'm trying to do here?CGRectIntersectsRect Logic - 有没有更好/更简单的方法来完成我在这里尝试做的事情?
【发布时间】:2023-04-09 09:19:01
【问题描述】:

这就是我想要做的..

我有所有字母的 UIImageViews。如果用户从字母表中向下平移一个字母以拼写一个单词,并且该字母被平移和丢弃,没有与任何其他字母相交,那么我认为他们没有拼写一个单词,所以我将此字母添加到临时字母数组。

假设他们用 5 个字母来做这件事。现在他们决定要使用他们已经筛选出的 5 个字母来构建单词。

我将如何创建一个 if 条件来检查是否有任何平移的字母与临时数组中存在的任何字母相交。

对于我正在尝试做的事情,似乎不能将我的 if 语句放在枚举 for 循环中。所以我不确定我能做什么。

这是我的伪代码+逻辑:http://pastie.org/2738238

if ([gestureRecognizer state] == UIGestureRecognizerStateEnded)
{

    //If a letter has never been panned or the currentPanned letter does not intersect with a letter that was added to the tempLetterArray
   //*letterintempLetterArray* = How do I test against all the letters in the array in the if condition?
    if ([tempLetterArray count] < 1 || !CGRectIntersectsRect(currentLetter.frame, *letterIntempLetterArray*.frame))
    {
        //Letters that may become words if other letter images are panned next to it.
        //Make the frame of letter larger so the frame intersects without having to overlap letter.
        currentLetter.contentMode = UIViewContentModeCenter;
        currentLetter.frame = CGRectInset(currentLetter.frame, -10, -10);

        currentLetter.backgroundColor = [UIColor redColor];
        [tempLetterArray addObject:currentLetter];  

    }

    //If a word hasn't been built yet, and user pans letter next to existing letter on the screen, build the first word.
    else if ([firstWordArray count] < 1 && CGRectIntersectsRect(currentLetter.frame, *letterIntempLetterArray*.frame))
    {

    //Align centers of the currentLetter and the *letterInTempWordArray* in which the currentLetter intersected with


        //Remove the letter from the temp array, and add it to the firstWordArray
        NSUInteger indexOfTempLetter = [tempLetterArray indexOfObject:*letterIntempLetterArray*];
        UIImageView *tempLetter = [[tempLetterArray objectAtIndex: indexOfTempLetter] retain];
        [tempLetterArray removeObjectAtIndex: indexOfTempLetter];
        [firstWordArray insertObject: tempLetter atIndex:0];
        [tempLetter release];

        //Make the frame of letter larger so the frame intersects without having to overlap letter.
        //Add the letter that was just dropped after
        currentLetter.contentMode = UIViewContentModeCenter;
        currentLetter.frame = CGRectInset(currentLetter.frame, -10, -10);

        currentLetter.backgroundColor = [UIColor redColor];
        [firstWordArray addObject: currentLetter];

    }

    //Start building the first word
    else if ([firstWordArray count] > 1 && CGRectIntersectsRect(currentLetter.frame, *letterInFirstWordArray*.frame))
    {
        //Align centers of the currentLetter and the *letterInFirstWordArray* in which the currentLetter intersected with
    //Do more stuff

    }

    //If the current letter intersects with a letter in temp letter array, and the first word array has already been built on
    //Start building the second word (Basically do this until 5 words have been created)
    else if ([firstWordArray count] > 1 && CGRectIntersectsRect(currentLetter.frame, *letterInTempLetterArray*.frame))
    {
    //Align centers of the currentLetter and the *letterInTempLetterArray* in which the currentLetter intersected with
    //Do more stuff
    }

}

【问题讨论】:

    标签: iphone objective-c ios ipad uiimageview


    【解决方案1】:
    BOOL touched = NO;
    
    for(UIImageView* img in TemporedArrayOfLetter)
    {
       touched = CGRectIntersectsRect(currentLetter.frame,img.frame);
       if(touched)break;
    }
    if(tapcount<1 && touched)
    {
      foo code;
    }
    

    希望对你有帮助。

    【讨论】:

    • 我爱你。这帮助很大。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多