2009-10-01 23 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]];