2
首先,這段代碼工作正常,它正是我所需要的,但是在審查之後,我錯過了一些東西。在屏幕上移動UIView,爲什麼這個工作?
我們有一張卡。當被觸摸時,卡片移動。在某個位置時,它會到達那個位置。
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
...
// Take note of the starting position and current position, both the same
startTouchPoint = pt;
currentTouchPoint = pt;
...
}
-(void) touchesMoved:(NSSet *) touches withEvent:(UIEvent *)event {
...
// As touch moves around the screen, current touch point is
// updated with it's new location
currentTouchPoint = [first locationInView:self];
...
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
...
// If something happens, put card in a given location
cardCurrentlyBeingTouched.xPosition = 300;
cardCurrentlyBeingTouched.yPosition = 150;
...
}
-(void) drawRect:(CGRect)rect {
...
// Card moving around the screen
cardCurrentlyBeingTouched.xPosition += (self.currentTouchPoint.x - startTouchPoint.x);
cardCurrentlyBeingTouched.yPosition += (self.currentTouchPoint.y - startTouchPoint.y);
CGPoint moveToLoc = CGPointMake(cardCurrentlyBeingTouched.xPosition, cardCurrentlyBeingTouched.yPosition);
[self addSubview:[cardCurrentlyBeingTouched uiImageViewAtPoint:moveToLoc rotated:0 withFaceImage:YES]];
startTouchPoint.x = currentTouchPoint.x;
startTouchPoint.y = currentTouchPoint.y;
...
}
所以,在drawRect
,(在任何時候)self.currentTouchPoint.x
相同startTouchPoint.x
(如Y),如何在世界上卡的X和Y位置正在發生變化,如果增量始終爲0?(X和Y的位置是CGPoints
,當我將它們打印進行比較時,我打印了浮標)
難道這是精確度問題嗎?
這是我的代碼,我寫它,它的工作原理。但爲什麼? :)