0

私はiPhone開発の初心者です(Xcodeで3日目)。ユーザーがさまざまなページ間をスワイプできるように、pageControlとscrollviewを実装しようとしています。このチュートリアルを使用していますが、ビューの背景色を変更するだけでなく、nib ファイルからビューをロード/切り替える方法がわかりません。どんな助けでも大歓迎です。

マイコード

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

1 に答える 1

0

Objective C へようこそ。

まず、スクロールビューのデリゲートを次のように設定する必要があります

//in .h file write

@interface ViewController : UIViewController<UIScrollViewDelegate>



// in viewDidLoad

self.scrollView.delegate = self;

次に、UIScrollView write のデリゲート メソッドに書き込みます...

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

次に、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];
}

そして、あなたはやった.......

これについてご不明な点がございましたら、お気軽にお問い合わせください......

于 2013-03-21T09:00:22.517 に答える