1

Here, the issue is nothing but I don't know much about objectiveC. So the query is that I am working on a project where user taps on Image and with UITapGestureRecognizer I have to store that tapped position in my array. I don't know how is this possible that every time user taps the view and CGPoint value gets stored in NSMutableArray.

dataArray = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < [getResults count]; i++) {

[dataArray addObject:tappedPoint];
NSLog(@"RESULT TEST %@", dataArray);
}
3
  • 6
    Anything you have tried to far? Commented Aug 2, 2014 at 5:55
  • THIS answers your question. You will have to try it yourself. Commented Aug 2, 2014 at 6:05
  • 1
    [yourArray addObject:[NSValue valueWithCGPoint: point]]; Commented Aug 2, 2014 at 6:14

1 Answer 1

1

You need to subclass UIView and implement the method - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event to get a touch callback. Second, a touch location (CGPoint) cannot be added to an NSMutableArray, but a wrapper class for it can (NSValue). Here would be a very basic implementation of what you are going for.

// in UIView subclass
// Precondition: you have an NSMutableArray named `someMutableArray'
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touchLocation = [[touches anyObject] locationInView:self];
    NSValue *wrapper = [NSValue valueWithCGPoint:touchLocation];

    [someMutableArray addObject:wrapper];
}

If you want to loop through these touch locations later, all you have to to is fast enumerate through the array and unwrap the NSValues.

for (NSValue *wrapper in someMutableArray) {
    CGPoint touchLocation = [wrapper CGPointValue];
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Brian Tracy, you mean that i don't need to implement any for loop,right?
@SushilSharma No, unless you want to loop through all of the touch locations later.
Ok,so with this code it will add objects one after another,every time user taps the view.
Thanks Brian Tracy,thanks a lot for your great solution.

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.