将UIView移动到另一个UIView

时间:2012-12-19 13:50:14

标签: iphone objective-c ios uiview

我有一个UIViewController,其中包含2个UIScrollViewsscrollView1scrollView2

scrollView1包含许多UIViews,当点击其中一个UIViews时,我希望它移入scrollView2

点击属于UIView的{​​{1}}时,会调用scrollView1内的方法,并将UIViewController作为参数传递。

2 个答案:

答案 0 :(得分: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;
                     }];

答案 1 :(得分:1)

假设您将这两个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];
}