2011-03-21 130 views
0

在我的應用我想(用下面的代碼從觸摸)到移動對象的紅色矩形內如何使這個iphone screen如何使利潤率

代碼:

UITouch *touch = [[event allTouches] anyObject]; 
CGPoint location = [touch locationInView:[touch view]]; 
if (insidethepaddle) { 
    object1.center = CGPointMake(location.x, location.y - 50); 
} 
+0

請澄清你的問題,至少我不明白你想達到什麼。 – 2011-03-21 19:59:02

+0

@Erik Aigner,我只想讓對象只在紅色矩形中移動,換句話說,我需要限制對象的協調(x,y) – 2011-03-21 20:10:02

回答

0

只需使用CGRectContainsPoint(CGRect rect, CGPoint point)NSPointInRect(NSPoint aPoint, NSRect aRect)來測試中心是否還在裏面,否則就不要更新對象center

指定的CGRect/NSRect表示紅色矩形的框架。

+0

我使用了你的代碼CGRect red = CGRectMake(40,80,300 ,440); \t如果(insidethepaddle && \t CGRectContainsPoint(紅,位置)){ \t \t = paddle.center CGPointMake(location.x,location.y - 50); } 它工作正常,但當我走出紅色的對象釋放如何CSN我解決這個問題 – 2011-03-21 20:35:41