【问题标题】:How to implement views to the pagecontrol如何实现页面控件的视图
【发布时间】:2011-01-14 21:33:19
【问题描述】:

我即将创建一个应用程序。

它应该有 3 个主要页面。所以我想用 PageControl 来实现这一点。

我创建了 3 个视图,现在我被困在这个 PageControl 的实现上。

有没有人可以看的好教程或示例代码(也可以是德语)?

谢谢, 迈克尔

【问题讨论】:

标签: iphone uiview uipagecontrol


【解决方案1】:

这里有一个简单的使用方法。

PageController.h:

#import <UIKit/UIKit.h>

@interface PageController : UIViewController {
    NSArray * views;
    UIPageControl *pageControl;
}

@property (nonatomic, retain) IBOutlet UIPageControl * pageControl;

- (IBAction) changePage:(id)sender;
- (void) animateToView:(UIView *)newView;

@end

PageController.m:

#import "PageController.h"

@implementation PageController

- (void)viewDidLoad {
    [super viewDidLoad];
    pageControl.numberOfPages = [views count];
    pageControl.currentPage = 0;

    // Either wire this up in Interface Builder or do it here.
    [pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
}

- (IBAction) changePage:(id)sender {
    UIView * newView = [views objectAtIndex:[pageControl currentPage]];
    [self animateToView:newView];
}

- (void) animateToView:(UIView *)newView {
     // You'd have to implement this yourself
}

@end

【讨论】:

  • 在 PageController.h 中更正:@property (nonatomic,retain) IBOutlet UIPageControl *pageControl;
  • 我在切换视图时遇到问题,这是我的代码:#import "ViewController.h" @implementation ViewController @synthesize pageControl; - (void)viewDidLoad { [super viewDidLoad]; //Create UIViews UIView *view1 = [[UIView alloc] init]; UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image1.jpg"]]; [view1 addSubview:imageView]; //ditto for image 2 [self.view addSubview:view1]; views = [[NSArray alloc] initWithObjects:view1, view2, nil]; pageControl.numberOfPages = [views count]; pageControl.currentPage = 0; } @end
  • 好吧,我用 3 个以编程方式创建的 UIImageViews 进行了设置,每个 UIImageViews 都有一个 UIImage。我理解调用 changePage 的概念并且它有效,但我知道我需要将第一个 uiimageview 放在主视图上,然后每次调用 changePage 方法时我都必须“将 320 px 添加到其位置,将其从视图中删除,添加320 像素到新视图”?
  • 是的,但与其硬编码 320,不如根据 superview 的边界计算位置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多