russell
russell

Reputation: 3766

Finding touch point in Objective c/cocoa touch

I have a problem to find the touch point.

I have a function loadView() where I set 16 tiles(16 UIImageView).

Then In function:

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    // Retrieve the touch point

    UITouch *touch=[[event allTouches]anyObject];
    CGPoint point= [touch locationInView:touch.view];
    CGRect frame = [self frame];  // Error this Line
}

I have Used frame to identify which frame/tiles is pressed using frame-origin.

But this line:

CGRect frame = [self frame];

makes me crazy. Plz someone tell me What to do.(with explanation and why not working). Plz.

Upvotes: 2

Views: 7068

Answers (3)

Osama Khalifa
Osama Khalifa

Reputation: 373

Use this :

UITouch *touch=[[event allTouches]anyObject];
CGPoint point= [touch locationInView:self.view];

Upvotes: 4

Moiz Ahmed
Moiz Ahmed

Reputation: 101

I am still a bit unclear on what you want, but let's see if this helps you out.

If you are moving different objects then why don't you create another class and inherit it with UIView and define the touch functions in the child class. Example Header

...
Tile : UIView
...

Example class implementation

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

}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:[self superview]];

}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:[self superview]];
    self.center = touchPoint;
}
...

or else if you just want the touch points in a UIViewController.

UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:[self.view]];

Upvotes: 1

Felixyz
Felixyz

Reputation: 19143

It seems like you're trying to do this in a method in a UIViewController subclass. The line should probably be:

[self.view frame];

Upvotes: 2

Related Questions