簡體   English   中英

將UIView移動到另一個UIView

[英]Move UIView to another UIView

我有一個UIViewController ,它包含2個UIScrollViewsscrollView1scrollView2

scrollView1包含許多UIViews ,拍打它的一個時UIViews我希望它移動到scrollView2

當點擊屬於scrollView1UIView ,將scrollView1 UIViewController內部的方法,並將view作為參數傳遞。

在那種方法中你應該寫如下:

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

編輯

對於動畫移動,您應該嘗試以下方法:

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

假設您將這兩個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