Ios 用我的手指移动一个视图

Ios 用我的手指移动一个视图,ios,uiview,move,touchesbegan,Ios,Uiview,Move,Touchesbegan,我正在尝试移动一个UIView(我们称之为viewA),它位于另一个UIView(我们称之为viewB)的顶部。viewB有iPad屏幕的大小,而VIEWA则小得多 我设法移动东西,但整个屏幕都在移动(viewA+viewB),而不仅仅是viewA 以下是我的viewA类代码: 我不明白的是,我在viewA类中实现了这些方法。“自我”应该关注这个观点,对吗?那么,当我的手指在屏幕上移动时,为什么整个页面都会移动呢?如果您正在移动包含所有视图的整个窗口,您应该只移动您想要的视图,在您的情况下是

我正在尝试移动一个UIView(我们称之为viewA),它位于另一个UIView(我们称之为viewB)的顶部。viewB有iPad屏幕的大小,而VIEWA则小得多

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

以下是我的viewA类代码:


我不明白的是,我在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;
}