Перетащите UIView между UIViews

У меня есть объект X UIView, который содержится в объекте A UIView. Я хочу иметь возможность прикоснуться к X, удалить его из объекта A и переместить в объект B (другой UIView). Оба объекта A и B находятся внутри одного супер-UIView.

  A        B
_____    _____
|   |    |   |
| X | -> |   |
|___|    |___|

Это то, что у меня есть.

@implementation X_UIView

float deltaX;
float deltaY;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.superview.superview addSubview:self]; //pop dragged view outside of container view

    CGPoint beginCenter = self.center;

    UITouch * touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.superview];

    deltaX = touchPoint.x - beginCenter.x;
    deltaY = touchPoint.y - beginCenter.y;
}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    UITouch * touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.superview];

    // Set the correct center when touched 
    touchPoint.x -= deltaX;
    touchPoint.y -= deltaY;

    self.center = touchPoint;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    //discover view that event ended was over and add self as a subview.
}

@end
8
задан Peter Hosey 1 October 2010 в 22:01
поделиться