Else-if语句似乎没有被执行

时间:2015-05-14 15:20:17

标签: ios objective-c if-statement

我有一个if语句,用于检查可拖动的imageView是否与另一个imageView相交 - 这会保留拖动到它的项目数量并更改图像。我还有一个else-if语句,以查看2 imageViews是否与计数器imageView相交。第一个if语句工作正常,但是当我去拖动另一个语句时,没有任何反应。图像没有变化。我尝试按照以下方式进行,但它似乎无法正常工作。

任何人都知道为什么以及是否有更好的方法来做到这一点?

我的代码:

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

    for (UIImageView *letter in letterA)
    {
        if (CGRectIntersectsRect(letter.frame ,answerA.frame))
        {
            UIImage *Pic1 = [UIImage imageNamed:@"number1"];
            [correctCounterA setImage:Pic1];
            letter.userInteractionEnabled=NO;
            letter.hidden=YES;

        }
        else if(CGRectIntersectsRect(letter.frame ,answerA.frame) && CGRectIntersectsRect(letter.frame, answerA.frame))
        {
            UIImage *Pic2 = [UIImage imageNamed:@"number2"];
            [correctCounterA setImage:Pic2];
            letter.userInteractionEnabled=NO;
            letter.hidden=YES;
        }
    }

}

3 个答案:

答案 0 :(得分:1)

你的第二个条件只涵盖了第一个条件的一个子集,所以它在第一种情况下已经是真的,或者永远不会......

您目前的问题是: 如果字母相交回答做某事。 如果字母不与答案相交,那么如果字母与答案相交并且字母相交,则答案会做其他事情。

我在这里错过了一些替代方案......

答案 1 :(得分:0)

如果目标是实现可拖动视图并检测交叉点,那么我会像这样处理问题:

@interface ViewController()
@property (weak, nonatomic) IBOutlet UIImageView *imageViewOne;
@property (weak, nonatomic) IBOutlet UIImageView *imageViewTwo;
@property (weak, nonatomic) IBOutlet UIImageView *imageViewThree;
@property (weak, nonatomic) IBOutlet UIImageView *imageViewFour;    
@property (nonatomic) BOOL hasView;
@property (strong, nonatomic) NSArray *imageViewCollection;
@property (strong, nonatomic) UIImageView *viewToDrag;
@property (weak, nonatomic) IBOutlet UIImageView *destinationView;
@property (nonatomic) NSUInteger destinationViewCounter;
@property (nonatomic) CGPoint originalLocation;
@end


- (void)viewDidLoad {
    [super viewDidLoad];
    self.imageViewCollection = @[_imageViewOne, _imageViewTwo, _imageViewThree, _imageViewFour];
    self.view.multipleTouchEnabled = NO;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    for(UIImageView *view in _imageViewCollection) {
        if(CGRectContainsPoint(view.frame, touchLocation)) {
            self.originalLocation = view.center;
            self.viewToDrag = view;
            self.hasView = YES;
        }
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    if(self.hasView) {
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint touchLocation = [touch locationInView:self.view];
        self.viewToDrag.center = touchLocation;
        [self.view bringSubviewToFront: self.viewToDrag];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if(CGRectIntersectsRect(self.destinationView.frame, self.viewToDrag.frame)) {
        NSUInteger tagNumber = self.viewToDrag.tag;
        [self removeImageForTagNumber:tagNumber];   //to remove imageViews after dragged
        self.viewToDrag.center = self.originalLocation;
        self.destinationViewCounter++;
        self.viewToDrag = nil;
        if(self.destinationViewCounter > 4) {
            self.destinationViewCounter = 0; //if we're out of range reset the counter
        }
        self.destinationView.image = [self imageForNumber:self.destinationViewCounter];   //convenience method to grab the correct image (correlating to the counter)
    }  
    self.viewToDrag.center = self.originalLocation; //snap the imageView back into place
    self.hasView = NO;
}

- (void)removeImageForTagNumber:(NSUInteger)number {
    switch (number) {
        case 1:
            [_imageViewOne removeFromSuperview];
            break;
        case 2:
            [_imageViewTwo removeFromSuperview];
            break;
        case 3:
            [_imageViewThree removeFromSuperview];
            break;
        case 4:
            [_imageViewFour removeFromSuperview];
        default:
            return;
    }
}

我还为您here

创建了一个示例项目

答案 2 :(得分:0)

看起来这就是你所追求的:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    int intersectionCount = 0;
    for (UIImageView *letter in letterA) {
        if (CGRectIntersectsRect(letter.frame ,answerA.frame)) {
            letter.userInteractionEnabled = NO;
            letter.hidden = YES;
            intersectionCount++;
        }
    }

    if (intersectionCount == 1) {
        UIImage *Pic1 = [UIImage imageNamed:@"number1"];
        [correctCounterA setImage:Pic1];
    }

    else if (intersectionCount == 2) {
        UIImage *Pic2 = [UIImage imageNamed:@"number2"];
        [correctCounterA setImage:Pic2];
    }
}

或者更一般地说:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    int intersectionCount = 0;
    for (UIImageView *letter in letterA) {
        if (CGRectIntersectsRect(letter.frame ,answerA.frame)) {
            letter.userInteractionEnabled = NO;
            letter.hidden = YES;
            intersectionCount++;
        }
    }

    if (intersectionCount == 0) {
        [correctCounterA setImage:nil];
    }

    else {
        NSString *imageName = [NSString
            stringWithFormat:@"number%d", intersectionCount];
        UIImage *image = [UIImage imageNamed:imageName];
        [correctCounterA setImage:image];
    }
}