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.
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;
}];
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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With