用我的手指移动UIView

时间:2012-05-02 09:37:07

标签: ios uiview move touchesbegan

我正在尝试移动一个UIView(让我们称之为viewA),它位于另一个UIView(让我们称之为viewB)之上。 viewB具有iPad屏幕的大小,而视图A则小得多。

我设法移动东西,但整个屏幕移动(viewA + viewB),而不仅仅是viewA。

这是我的viewA类代码:


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:self->view]; if (CGRectContainsPoint(self.window.frame, touchLocation)) { dragging = YES; oldY = touchLocation.y; } }

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:self->view]; if (dragging) { CGRect frame = self.window.frame; frame.origin.y = self.window.frame.origin.y + touchLocation.y - oldY; self.window.frame = frame; } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { dragging = NO; }

我不明白的是,我在viewA类中实现了这些方法。 “自我”应该关注这个观点,对吗?那么,当我的手指在屏幕上移动时,为什么整个页面会移动?

1 个答案:

答案 0 :(得分:2)

您正在移动包含所有视图的整个窗口,您只应移动所需的视图,在您的情况下查看。