我正在嘗試移動與用戶觸摸相關的UIView。移動UIView與觸摸相關
這就是我目前所面對的:
int oldX, oldY;
BOOL dragging;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(window.frame, touchLocation)) {
dragging = YES;
oldX = touchLocation.x;
oldY = touchLocation.y;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(window.frame, touchLocation) && dragging) {
CGRect frame;
frame.origin.x = (window.frame.origin.x + touchLocation.x - oldX);
frame.origin.y = (window.frame.origin.y + touchLocation.y - oldY);
window.frame = frame;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
dragging = NO;
}
的觀點不斷閃爍的從一個地方到另一個,我不知道自己還能做些什麼。
任何幫助表示讚賞。
窗口是你拖動的對象嗎? – EmptyStack 2010-12-20 11:50:27
是的,它是在Interface Builder中創建的視圖。 – 2010-12-20 11:54:51
它好多了,但每當它被觸碰時都會奇怪地縮到一邊。 – 2010-12-20 12:47:18