2009-10-01 18 views
2

我找到触点的问题。在Objective c中找到触摸点c/cocoa touch

我有一个功能loadView()其中我设置了16个瓷砖(16 UIImageView)。

然后在功能:

- (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 
} 

我已经使用帧识别哪些帧/瓦片使用帧起源按压。

,但此行:

CGRect frame = [self frame]; 

让我疯了。有人告诉我该怎么做(有解释,为什么不工作)。 PLZ。

+1

你为什么不告诉我们你得到了什么样的错误?同样,告诉我们这个方法在 – newacct 2009-10-01 07:55:08

+0

中是什么类的。是的,你需要提供更多的细节让我们能够解决你的问题。 – 2009-10-01 12:15:01

回答

2

看来你正在尝试在UIViewController子类的方法中做到这一点。该生产线大概应该是:

[self.view frame]; 
4

使用此:

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

我还是你想要什么有点不清楚,但是让我们看看这可以帮助你。

如果您正在移动不同的对象,那么为什么不创建另一个类并使用UIView继承它并在子类中定义触摸功能。 举例标题

... 
Tile : UIView 
... 

例类实现

... 
-(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; 
} 
... 

否则,如果你只是想在一个UIViewController触摸点。

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