简体   繁体   English

将UIView移动到另一个UIView

[英]Move UIView to another UIView

I have an UIViewController which contains 2 UIScrollViews , scrollView1 , scrollView2 for instance. 我有一个UIViewController ,它包含2个UIScrollViewsscrollView1scrollView2

scrollView1 contains many UIViews , when tapping one of it's UIViews I want it to move into scrollView2 scrollView1包含许多UIViews ,拍打它的一个时UIViews我希望它移动到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. 当点击属于scrollView1UIView ,将scrollView1 UIViewController内部的方法,并将view作为参数传递。

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: 假设您将这两个scrollView声明为属性:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM