0

I'm a beginner in learning objective c. I want to make my iphone app do this thing:

  • if touch A area, do XXXXX
  • if touch B area, do YYYYY
  • if touch A&B areas at same time, do ZZZZZZ

I think the first thing I need to do is to save the Coordinate of every touch, and then to check all the coordinates in the right area or not.

I Use NSMutableArray to save the Coordinate, but I don't know how to get the content in the array.

Here is my code:

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

    NSMutableArray *Xarray;
    NSMutableArray *Yarray;

    Xarray=[NSMutableArray arrayWithCapacity:[touches count]];
    Yarray=[NSMutableArray arrayWithCapacity:[touches count]];


    for(UITouch *touch in touches)
    {   
        CGPoint pstart=[touch locationInView:self.view];
        [Xarray addObject:[NSNumber numberWithFloat:pstart.x]];
        [Yarray addObject:[NSNumber numberWithFloat:pstart.y]];
    }    
}

thanks very much!

2
  • Why do you need to store the touches at all; why not fire your XXXXX, YYYYY, ZZZZZ from within the touchesBegin method? I think your solution will be expensive anyway, creating and populating an object each time the screen is touched (not to mention what looks like a memory leak using arrayWithCapacity rather than initWithCapacity). Commented May 11, 2012 at 10:05
  • I got the sense! thanks you all very well! Commented May 11, 2012 at 11:06

1 Answer 1

1

NSMutableArray is a subclass of NSArray, whose documentation is at https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/cl/NSArray where you'll find, e.g., that the method for accessing an element in an array is called objectAtIndex:. So, e.g., Xarray objectAtIndex:0 gets the first element in the array. There are also methods for extracting multiple elements at once, iterating over all the objects in the array, etc.

For your application you might actually want indexOfObjectPassingTest: (the test would be looking for positions within a given area).

Sign up to request clarification or add additional context in comments.

2 Comments

NSLog(@"x:%d,y:%d",[Xarray objectAtIndex:i],[Yarray objectAtIndex:i]); I have tried this code to check my coordinate is correct or not.But the result is not my thought. The outputs always like this"x:225058736,y:225058752" I don't know why.. very thanks!
The things returned by objectAtIndex: are objects, not primitive types. In this case they're NSNumbers. (An NSArray can only hold objects, not ints or floats or whatever.) If you want to format them with %d then you need to extract an int value with intValue, though I notice that the numbers you're actually putting in there are floating-point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.