Search code examples
iosuiviewmovetouchesbegan

Move a UIView with my finger


I'm trying to move a UIView (let's call it viewA), which is located on top of another UIView (let's call it viewB). viewB has the size of the iPad screen, and view A is much much smaller.

I manage to move things, but the whole screen moves (viewA + viewB), not only viewA.

Here is my code for viewA class:


- (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; }

What I do not understand, is that, I implemented these methods in the viewA class. "Self" should concern this viewA, right? So why does the whole page move when my finger move on the screen?


Solution

  • You are moving the entire window, which contains all the views, you should only move the view you want, viewA in your case.