Finding a touch point in Objective c / cocoa touch

I have a problem to find the touch point.

I have a function loadView()

where I am setting 16 plates (16 UIImageView

).

Then in the 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 used a frame to determine which frame / tile is pressed using the start of the frame.

But this line:

CGRect frame = [self frame];

      

makes me crazy. Plz someone tell me what to do (with explanation and why doesn't work). Plz.

+2


source to share


3 answers


Use this:



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

      

+4


source


It looks like you are trying to do this in a method in your UIViewController subclass. The line should be:



[self.view frame];

      

+2


source


I'm still a bit confused about what you want, but let's see if that helps you.

If you are moving different objects, then why not create another class and inherit using UIView and define touch functions in the child class. Example title

...
Tile : UIView
...

      

Example of 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 if you just want to use touch points in the UIViewController.

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

      

0


source







All Articles