2015-02-12 71 views
-1

下面的代碼無法正常工作(這是touchesBegan減法不工作

​​

那麼這75%的事實。當我點擊屏幕的右上象限時,每個變量的結果都很好,但是不在圖表中(通常大於或等於1800000000)。誰能幫我嗎?我不明白爲什麼會出錯。

+0

你爲什麼要轉換成'NSUInteger'?爲什麼不使用'CGFloat'? – rakeshbs 2015-02-12 01:33:12

+1

向我們展示正在打印的值。 – 2015-02-12 01:41:07

回答

2

爲什麼要轉換爲NSUInteger來計算斜率?改爲使用CGFloat

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

CGPoint playerPoint = CGPointMake(100, 100); 

CGFloat a = touchLocation.y; 
CGFloat b = touchLocation.x; 
CGFloat c = playerPoint.y; 
CGFloat d = playerPoint.x; 

NSLog(@"%f", b); 
NSLog(@"%f", a); 
NSLog(@"%f", d); 
NSLog(@"%f", c); 

CGFloat offSetY = a - c; 
CGFloat offSetX = b - d; 
CGFloat slope = (offSetY/offSetX); 

NSLog(@"%f", offSetX); 
NSLog(@"%f", offSetY); 
NSLog(@"%f", slope);