2012-12-19 133 views
3

我有一個UIViewController其中包含2 UIScrollViews,scrollView1,scrollView2例如。移動UIView到另一個UIView

scrollView1包含許多UIViews,敲擊的時候它的一個UIViews我希望它移動到scrollView2

當輕敲UIView屬於scrollView1UIViewController內的方法被調用,view爲傳遞參數。

回答

11

在你應該寫這樣的事情方法:

[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; 
        }]; 
+0

謝謝,但我希望視圖實際移動到scrollView2使用動畫,我怎麼能結合這在動畫? – jkigel

1

假如你有這兩個scrollViews聲明爲屬性:

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