如何使用三个选项卡进行视差演示,如下图所示

时间:2015-11-16 12:11:38

标签: ios objective-c iphone

如何使用下面在iOS中给出的三个标签图像进行视差演示,使其垂直滚动。点击多个表格滚动显示Horizental。

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试使用启用了分页的UIScrollView。然后将IBAction连接到带有标记值的按钮。标记值指示UIScrollView必须显示的页面。确保UIScrollView方向设置为水平。在这个例子中,我只是添加了UIViews,但这可能是任何东西。您可以使用UITableView,UIScrollView或UICollectionView命名。

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

//Set number of pages
   int numberOfPages = 3;

//Set properties scrollView
   self.scrollView.pagingEnabled = true;
   self.scrollView.contentSize = CGSizeMake((numberOfPages * self.scrollView.frame.size.width), self.scrollView.frame.size.height);

//Add pages
   UIView *pageOne = [[UIView alloc] initWithFrame:CGRectMake((0 * self.scrollView.frame.size.width), 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
   [pageOne setBackgroundColor:[UIColor yellowColor]];
   [self.scrollView addSubview:pageOne];

   UIView *pageTwo = [[UIView alloc] initWithFrame:CGRectMake((1 * self.scrollView.frame.size.width), 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
   [pageTwo setBackgroundColor:[UIColor blueColor]];
  [self.scrollView addSubview:pageTwo];

   UIView *pageThree = [[UIView alloc] initWithFrame:CGRectMake((2 * self.scrollView.frame.size.width), 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
   [pageThree setBackgroundColor:[UIColor redColor]];
   [self.scrollView addSubview:pageThree];
}

- (IBAction)btnSelectPage:(id)sender; {
    UIButton *btn = (UIButton *)sender;
    [self.scrollView setContentOffset:CGPointMake((self.scrollView.frame.size.width * btn.tag), 0) animated:true];
}

为故事板做这样的事情: Storyboard layout

不要忘记设置按钮的标记值,指示您要显示的页面。 enter image description here