0

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?

1 Answers1

0

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

}
spring
  • 18,009
  • 15
  • 80
  • 160
  • Thanks for the quik response! Look, only have to verify if the touch point is in one of the two last UIImageViews of ArrayImages1 (one from here) and ArrayImages2 (the other one from here), because of that i maked the "posible" UIImageView (to point to the last one of both arrays and check if the user touched one). I dont think i have to search in all the elements of the Array, just in the last one visible (and i know its Contador1-1) – Eitán José Teplitzky Jul 27 '12 at 23:53
  • The logic in the code you posted is hard to follow so I can't hazard a guess. Simplifying that, you can get the last object in your array by using the array function [array lastObject]. As to the crash, I guess I would check if 'posible' is nil, and also examine the crash log/error to see if you can pinpoint the cause. – spring Jul 28 '12 at 00:04
  • I solved the issue making two NSMutableArray and using the method lastObject, thanks skinnyTOD – Eitán José Teplitzky Jul 28 '12 at 13:49