反转UIImage的变化

时间:2014-04-30 12:52:58

标签: ios objective-c uiimage

我有一个图像视图,可以旋转图像以面向不同的方向。我试图找出按下按钮时如何反转旋转。图像视图也可以向前移动,我已经找到了如何扭转这一举动。我基本上想做我在倒车前进动作时所做的事情,除非是为了转动图像。

前进代码:

- (IBAction)moveForward:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Illegal Move!" message:@"You can't move forward" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    UIAlertView *stoneWallAlert = [[UIAlertView alloc]initWithTitle:@"Path Blocked" message:@"There is a stone wall in your way!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];

    BOOL fullyContained = CGRectContainsRect(gameBoard.bounds, turtle.frame);

    moveForwardCards--;
    forwardLabel.text = [NSString stringWithFormat:@"%i left", moveForwardCards];

    if (moveForwardCards == 0)
    {
        forwardButton.enabled = NO;
    }

    if (turtle.image.imageOrientation == UIImageOrientationRight)
    {
        // If turtle will stay within the game board when moved, move it forward
        if (fullyContained)
        {
            turtle.frame = CGRectMake(turtle.frame.origin.x, turtle.frame.origin.y + 90, 75, 75);
            NSString *addY = @"y+90";
            [previousMoves addObject:addY];
            fullyContained = CGRectContainsRect(gameBoard.bounds, turtle.frame);

            // If turtle will move outside of game board when moved, do not move it
            if (!fullyContained)
            {
                turtle.frame = CGRectMake(turtle.frame.origin.x, turtle.frame.origin.y - 90, 75, 75);
                [previousMoves removeLastObject];
                [alert show];
                moveForwardCards++;
                forwardLabel.text = [NSString stringWithFormat:@"%i left", moveForwardCards];
            }

            // Loop through array of stone walls and check for an intersection with the turtle.
            // If there is an intersection, do not move the turtle
            for (UIImageView *stoneWall in self.stoneWallsArray)
            {
                if (CGRectIntersectsRect(turtle.frame, stoneWall.frame))
                {
                    turtle.frame = CGRectMake(turtle.frame.origin.x, turtle.frame.origin.y - 90, 75, 75);
                    [previousMoves removeLastObject];
                    [stoneWallAlert show];
                    moveForwardCards++;
                    forwardLabel.text = [NSString stringWithFormat:@"%i left", moveForwardCards];
                }
            }
        }
    }
    else if (turtle.image.imageOrientation == UIImageOrientationLeft)
    {
        // If turtle will stay within the game board when moved, move it forward
        if (fullyContained)
        {
            turtle.frame = CGRectMake(turtle.frame.origin.x, turtle.frame.origin.y - 90, 75, 75);
            NSString *minusY = @"y-90";
            [previousMoves addObject:minusY];
            fullyContained = CGRectContainsRect(gameBoard.bounds, turtle.frame);

            // If turtle will move outside of game board when moved, do not move it
            if (!fullyContained)
            {
                turtle.frame = CGRectMake(turtle.frame.origin.x, turtle.frame.origin.y + 90, 75, 75);
                [previousMoves removeLastObject];
                [alert show];
                moveForwardCards++;
                forwardLabel.text = [NSString stringWithFormat:@"%i left", moveForwardCards];
            }

            // Loop through array of stone walls and check for an intersection with the turtle.
            // If there is an intersection, do not move the turtle
            for (UIImageView *stoneWall in self.stoneWallsArray)
            {
                if (CGRectIntersectsRect(turtle.frame, stoneWall.frame))
                {
                    turtle.frame = CGRectMake(turtle.frame.origin.x, turtle.frame.origin.y + 90, 75, 75);
                    [previousMoves removeLastObject];
                    [stoneWallAlert show];
                    moveForwardCards++;
                    forwardLabel.text = [NSString stringWithFormat:@"%i left", moveForwardCards];
                }
            }
        }
    }
    else if (turtle.image.imageOrientation == UIImageOrientationUp)
    {
        // If turtle will stay within the game board when moved, move it forward
        if (fullyContained)
        {
            turtle.frame = CGRectMake(turtle.frame.origin.x + 90, turtle.frame.origin.y, 75, 75);
            NSString *addX = @"x+90";
            [previousMoves addObject:addX];
            fullyContained = CGRectContainsRect(gameBoard.bounds, turtle.frame);

            // If turtle will move outside of game board when moved, do not move it
            if (!fullyContained)
            {
                turtle.frame = CGRectMake(turtle.frame.origin.x - 90, turtle.frame.origin.y, 75, 75);
                [previousMoves removeLastObject];
                [alert show];
                moveForwardCards++;
                forwardLabel.text = [NSString stringWithFormat:@"%i left", moveForwardCards];
            }

            // Loop through array of stone walls and check for an intersection with the turtle.
            // If there is an intersection, do not move the turtle
            for (UIImageView *stoneWall in self.stoneWallsArray)
            {
                if (CGRectIntersectsRect(turtle.frame, stoneWall.frame))
                {
                    turtle.frame = CGRectMake(turtle.frame.origin.x - 90, turtle.frame.origin.y, 75, 75);
                    [previousMoves removeLastObject];
                    [stoneWallAlert show];
                    moveForwardCards++;
                    forwardLabel.text = [NSString stringWithFormat:@"%i left", moveForwardCards];
                }
            }
        }
    }
    else if (turtle.image.imageOrientation == UIImageOrientationDown)
    {
        // If turtle will stay within the game board when moved, move it forward
        if (fullyContained)
        {
            turtle.frame = CGRectMake(turtle.frame.origin.x - 90, turtle.frame.origin.y, 75, 75);
            NSString *minusX = @"x-90";
            [previousMoves addObject:minusX];
            fullyContained = CGRectContainsRect(gameBoard.bounds, turtle.frame);

            // If turtle will move outside of game board when moved, do not move it
            if (!fullyContained)
            {
                turtle.frame = CGRectMake(turtle.frame.origin.x + 90, turtle.frame.origin.y, 75, 75);
                [previousMoves removeLastObject];
                [alert show];
                moveForwardCards++;
                forwardLabel.text = [NSString stringWithFormat:@"%i left", moveForwardCards];
            }

            // Loop through array of stone walls and check for an intersection with the turtle.
            // If there is an intersection, do not move the turtle
            for (UIImageView *stoneWall in self.stoneWallsArray)
            {
                if (CGRectIntersectsRect(turtle.frame, stoneWall.frame))
                {
                    turtle.frame = CGRectMake(turtle.frame.origin.x + 90, turtle.frame.origin.y, 75, 75);
                    [previousMoves removeLastObject];
                    [stoneWallAlert show];
                    moveForwardCards++;
                    forwardLabel.text = [NSString stringWithFormat:@"%i left", moveForwardCards];
                }
            }
        }
    }

    NSLog(@"turtle frame: %@", NSStringFromCGRect(turtle.frame));
}

转弯代码:

- (IBAction)turnLeft:(id)sender
{
    turnLeftCards--;
    leftLabel.text = [NSString stringWithFormat:@"%i left", turnLeftCards];
    if (turnLeftCards == 0)
    {
        leftButton.enabled = NO;
    }

    UIImage *newImage;
    switch (turtle.image.imageOrientation)
    {
        case UIImageOrientationUp:
            newImage = [UIImage imageWithCGImage:[turtle.image CGImage] scale:1.0f orientation:UIImageOrientationLeft];
            turtle.contentMode = UIViewContentModeScaleAspectFit;
            break;
        case UIImageOrientationLeft:
            newImage = [UIImage imageWithCGImage:[turtle.image CGImage]scale:1.0f orientation:UIImageOrientationDown];
            turtle.contentMode = UIViewContentModeScaleAspectFill;
            break;
        case UIImageOrientationDown:
            newImage = [UIImage imageWithCGImage:[turtle.image CGImage] scale:1.0f orientation:UIImageOrientationRight];
            turtle.contentMode = UIViewContentModeScaleAspectFit;
            break;
        case UIImageOrientationRight:
            newImage = [UIImage imageWithCGImage:[turtle.image CGImage]scale:1.0f orientation:UIImageOrientationUp];
            turtle.contentMode = UIViewContentModeScaleAspectFill;
            break;
        default:
            break;
    }
    turtle.image = newImage;
}

逆转前进的代码:

- (IBAction)bugTapped:(id)sender
{
    // If the last move made the turtle move up on the y-axis, reverse that move
    if ([previousMoves.lastObject isEqual:@"y+90"])
    {
        turtle.frame = CGRectMake(turtle.frame.origin.x, turtle.frame.origin.y - 90, 75, 75);
        [previousMoves removeLastObject];
        moveForwardCards++;
        forwardLabel.text = [NSString stringWithFormat:@"%i left", moveForwardCards];
    }
    // If the last move made the turtle move down on the y-axis, reverse that move
    else if ([previousMoves.lastObject isEqual:@"y-90"])
    {
        turtle.frame = CGRectMake(turtle.frame.origin.x, turtle.frame.origin.y + 90, 75, 75);
        [previousMoves removeLastObject];
        moveForwardCards++;
        forwardLabel.text = [NSString stringWithFormat:@"%i left", moveForwardCards];
    }
    // If the last move mad the turtle move left on the x-axis, reverse that move
    else if ([previousMoves.lastObject isEqual:@"x+90"])
    {
        turtle.frame = CGRectMake(turtle.frame.origin.x - 90, turtle.frame.origin.y, 75, 75);
        [previousMoves removeLastObject];
        moveForwardCards++;
        forwardLabel.text = [NSString stringWithFormat:@"%i left", moveForwardCards];
    }
    // If the last move made the turtle move right on the x-axis, reverse that move
    else if ([previousMoves.lastObject isEqual:@"x-90"])
    {
        turtle.frame = CGRectMake(turtle.frame.origin.x + 90, turtle.frame.origin.y, 75, 75);
        [previousMoves removeLastObject];
        moveForwardCards++;
        forwardLabel.text = [NSString stringWithFormat:@"%i left", moveForwardCards];
    }
}

0 个答案:

没有答案