在ScrollView中移动子视图

时间:2013-10-09 04:48:38

标签: iphone ios objective-c uiscrollview views

所以我有一个滚动视图,其他3个视图作为子视图。使用滚动视图在3个子视图之间进行分页,这些子视图的控制器存储在数组中。

- (void)viewDidLoad
{  
    [self loadData];

    self.uiScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    [self.uiScrollView setPagingEnabled:YES];
    [self.uiScrollView setContentSize:CGSizeMake(self.view.frame.size.width*3, self.view.frame.size.height)];

    self.uiScrollView.delegate = self;


    [self.view addSubview:self.uiScrollView];

    self.uiPhotoCollectionViewControllers = [[NSMutableArray alloc] init];
    int numberOfOtherViews = 3;
    int subViewOffset = 0;

    for (int i = 0; i < numberOfCollectionViews; i++)
    {
        UIViewController* uiViewController = [[UIPhotoCollectionViewController alloc] initWithData:dataArray];
        // add offset to the view of the controller
        [self.uiScrollView addSubview:uiViewController.view];
        [self.uiViewControllers addObject:uiViewController];
        uiViewController.view.frame = CGRectOffset(uiViewController.view.frame,subViewOffset,0.0f);
        subViewOffset = subViewOffset + uiViewController.view.frame.size.width;
    }

如果我后来想要在第一个滚动视图中间切换视图,我将如何在不删除所有滚动视图子视图并替换它们的情况下进行此操作。

1 个答案:

答案 0 :(得分:0)

没有替换是不可能的,但你可以通过像这样交换self.uiViewControllers数组中的对象来做到这一点

[scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

然后在交换对象后,您可以通过遍历整个数组来添加子视图。