简体   繁体   English

如何使用PageControl和UIScrollView切换视图?

[英]How Do I Switch Views With PageControl & UIScrollView?

I'm a noob to Iphone development (3rd day in Xcode) and I am trying to implement pageControl and scrollview so users can swipe between various pages. 我是Iphone开发的新手(在Xcode中是第三天),我正在尝试实现pageControl和scrollview,以便用户可以在各个页面之间滑动。 I'm using this tutorial and I can't figure out how to load/switch views from nib files as opposed to just changing the background color of a view. 我正在使用教程,所以我不知道如何从nib文件加载/切换视图,而不是仅仅更改视图的背景色。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

MY CODE 我的密码

Modification of PageControlExampleViewController.m renamed NewsClass2 将PageControlExampleViewController.m的修改重命名为NewsClass2

// Creates the color list the first time this method is invoked. Returns one color object from the list.
+ (UIColor *)pageControlColorWithIndex:(NSUInteger)index {
if (__pageControlColorList == nil) {
    __pageControlColorList = [[NSArray alloc] initWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor magentaColor],
                              [UIColor blueColor], [UIColor orangeColor], [UIColor brownColor], [UIColor grayColor], nil];
}
// Mod the index by the list length to ensure access remains in bounds.
return [__pageControlColorList objectAtIndex:index % [__pageControlColorList count]];
}

//Changing views instead of colors, not working
+ (UIView *)pageControlViewWithIndex:(NSUInteger)index {
if (__pageControlViewrList == nil) {
    __pageControlViewrList = [[NSArray alloc] initWithObjects:[[UIView alloc] initWithNibName:@"PageView" bundle:nil], [[UIView alloc] initWithNibName:@"PageView" bundle:nil], [[UIView alloc] initWithNibName:@"PageView" bundle:nil],
                              [[UIView alloc] initWithNibName:@"PageView" bundle:nil], [[UIView alloc] initWithNibName:@"PageView" bundle:nil], [[UIView alloc] initWithNibName:@"PageView" bundle:nil], [[UIView alloc] initWithNibName:@"PageView" bundle:nil], nil];
}
// Mod the index by the list length to ensure access remains in bounds.
return [__pageControlViewList objectAtIndex:index % [__pageControlViewList count]];
}

// Set the label and background color when the view has finished loading.
- (void)viewDidLoad {
pageNumberLabel.text = [NSString stringWithFormat:@"Page %d", pageNumber + 1];
self.view.backgroundColor = [NewsClass2 pageControlColorWithIndex:pageNumber];
//Setting View Not Working
self.view = [NewsClass2 pageControlViewWithIndex:pageNumber];
}

welcome to Objective C. 欢迎来到目标C。

First of all you need to set delegate of scroll view like 首先,您需要设置滚动视图的委托,例如

//in .h file write

@interface ViewController : UIViewController<UIScrollViewDelegate>



// in viewDidLoad

self.scrollView.delegate = self;

then write in delegate method of UIScrollView write... 然后写在UIScrollView的委托方法中...

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = page;
}

then make an IBAction of valueChange for pageControl like ..... 然后为pageControl做一个valueChange的IBAction,如...

- (IBAction)changePage:(id)sender
{
    int page = self.pageControlHelp.currentPage;
    CGRect frame = self.scrollViewHelp.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    [self.scrollViewHelp scrollRectToVisible:frame animated:YES];
}

And you have done ....... 而且您已经完成了.......

If you have any confusion for this please feel free to ask...... 如果对此有任何困惑,请随时询问......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM