我正在尝试移动与用户触摸相关的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