Question

I have an NSArray whit 15 UIImageViews:

@interface ViewController : UIViewController{

    NSArray *ArrayImages1;
    NSArray *ArrayImages2;

}

in viewDidLoad:

 ArrayImages1 = [[NSArray alloc] initWithObjects: a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, nil];

Where a1, a2... are the outlets of the UIImageViews And the same for ArrayImages2

TouchesBegan and TouchesMoved:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    UIImageView *posible;
    int imagen;
    if (point.x < 161) {
        imagen = Contador1;
        if (Contador1 > 14) {imagen = Contador1 - 15;}
        imagen--;
        posible = [ArrayImages1 objectAtIndex:imagen];
    }
    else if (point.x > 159) {
        imagen = Contador2;
        if (Contador2 > 14) {imagen = Contador2 - 15;}
        imagen--;
        posible = [ArrayImages2 objectAtIndex:imagen];
    }
    if ([touch view] != posible) { return; }
    original2 = posible.center;    
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    UIImageView *posible;
    int imagen;
    if (point.x < 161) {
        imagen = Contador1;
        if (Contador1 > 14) {imagen = Contador1 - 15;}
        imagen--;
        posible = [ArrayImages1 objectAtIndex:imagen];
    }
    else if (point.x > 159) {
        imagen = Contador2;
        if (Contador2 > 14) {imagen = Contador2 - 15;}
        imagen--;
        posible = [ArrayImages2 objectAtIndex:imagen];
    }
    if ([touch view] != posible) { return; }
    posible.center = point;
}

I have to know if the touch was in one of the two posible UIImageViews, and if it was, move it. The Contador1 and Contador2 ints, are counters that counts how many UIImageView are visible, the user only can move the last 2 of them.

It works, the thing is when i touch outside, it makes the app crash. If i change in touchesBegan and TouchesMoved, the index of "posible = [ArrayImage..", for 0, it only works for the first UIImageView (i understand why), but it doesent crash.

Any ideas?

Was it helpful?

Solution

I have to know if the touch was in one of the two possible UIImageViews

I can't follow your code very well but it seems like you could simplify things if you first check if the point is within the rect of a view. Then base your logic off of the result. Something like

Using CGRectContainsPoint:

bool CGRectContainsPoint (
   CGRect rect,
   CGPoint point
);



UIImageView *foundView

for(UIImageView* theView in ArrayImages1){

 if (CGRectContainsPoint(theView.frame, touchPoint){
     foundView = theView;
     break;
 }

}

Then...

if (foundView != nil){
// do some logical thing

}

Or...

if ([foundView isEqual:someOtherView]){
// I may have the syntax wrong on the above

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top