Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move UIView to another UIView

I have an UIViewController which contains 2 UIScrollViews, scrollView1, scrollView2 for instance.

scrollView1 contains many UIViews, when tapping one of it's UIViews I want it to move into scrollView2

When tapping a UIView that belongs to scrollView1, a method inside the UIViewController is being called and the view is passed as a parameter.

like image 989
jkigel Avatar asked Dec 09 '22 18:12

jkigel


2 Answers

In that method you should write something like:

[view removeFromSuperview];
[scrollView2 addSubview:view];

Edit:

For animated move you should try something like:

CGPoint originalCenter = [self.view convertPoint:view.center fromView:scrollView1];
[view removeFromSuperView];
[self.view addSubview:view];
view.center = originalCenter;

CGPoint destinationPointInSecondScrollView = ; // Set it's value
CGPoint finalCenter = [self.view convertPoint:destinationPointInSecondScrollView fromView:scrollView2];
[UIView animateWithDuration:0.3
                      delay:0
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     view.center = finalCenter;
                 } completion:^(BOOL finished) {
                         [view removeFromSuperView];
                         [scrollView2 addSubview:view];
                         view.center = destinationPointInSecondScrollView;
                     }];
like image 60
Levi Avatar answered Dec 26 '22 14:12

Levi


Supposing you have these two scrollViews declared as properties:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)]
    for (UIView *view in self.scrollView1.subviews) {
        [view addGestureRecognizer:gesture];
    }
}

- (void)viewTapped:(UITapGestureRecognizer *)gesture
{
    UIView *view = gesture.view;
    [self moveToScrollView2:view];
}

- (void)moveToScrollView2:(UIView *)view
{
    [view removeFromSuperview];
    [self.scrollView2 addSubview:view];
}
like image 45
Lucion Hades Avatar answered Dec 26 '22 15:12

Lucion Hades