Daniel
Daniel

Reputation: 7724

Move UIView inside another UIView

How can I move an UIView A inside a square (that is another UIView B that isn't A's parent) ?


What I did: I overrode methods TouchesBegan, TouchesMoved and TouchesEnded to move A, and when A's left coincides with B's left, I avoided A to move left, but sometimes this causes A to move strangely and not smoothly (same thing for up, down and right directions), so I guess there must be a better way to do it.

Upvotes: 1

Views: 166

Answers (1)

Baig
Baig

Reputation: 4995

If you want ViewA draggable only inside the region of a specific view. This might be easier to define if your boundaries are complicated.

- (IBAction)handleDrag:(UIButton *)sender forEvent:(UIEvent *)event 
{   
    CGPoint point = [[[event allTouches] anyObject] locationInView:self.someView];

    if ([self.viewB pointInside:point withEvent:nil])
    {
        sender.center = point; 
        //Only if the gesture center is inside the specified view will the button be moved
    }
}

Upvotes: 3

Related Questions