通过数组进行页面控制

发布于 2024-12-13 05:35:02 字数 109 浏览 0 评论 0原文

我有兴趣使用页面控件来切换数组中的条目而不更改视图。我该怎么做?有教程吗。 。 。

然而,每当页面切换时,背景颜色和一些图像就会改变。有没有办法在页面切换后运行代码?

谢谢

I am interested in using a page control to switch through entries in my array without changing the view. How can I do this? Is there a tutorial. . .

However, whenever the pages is switched, the background color and some images change. Is there any way to run code once a page is switched?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§普罗旺斯的薰衣草 2024-12-20 05:35:02

UIPageControl代表分页,它本身不管理分页行为。单击 UIPageControl 时,您需要编写自己的代码来迭代数组。 currentPage 属性应设置为您在数组中使用的索引,totalPages 属性应设置为数组的 count 属性。最后,您需要使用 -addTarget:action:forControlEvents:

[pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];

文档状态

当用户点击页面控件移至下一页或上一页时,该控件会发送 UIControlEventValueChanged 事件以供委托处理。然后,委托可以评估 currentPage 属性来确定要显示的页面。页面控件仅向任一方向前进一页。

因此,您可以使用该事件来确定页面何时更改。

示例:

@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (strong, nonatomic) NSArray *items;

[...]

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.items = [NSArray arrayWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight",@"Nine",@"Ten", nil];
    [self.pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];

    // Left Swipe to go to previous page
    UISwipeGestureRecognizer *leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(previousPage:)];
    leftSwipeGestureRecognizer.direction = numberOfTouchesRequired = 1;
    leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:leftSwipeGestureRecognizer];

    // Right Swipe to go to next page
    UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(nextPage:)];
    rightSwipeGestureRecognizer.direction = numberOfTouchesRequired = 1;
    rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:rightSwipeGestureRecognizer];

}

- (void)previousPage:(UISwipeGestureRecognizer *)swipeGestureRecognizer
{
    self.pageControl.currentPage = self.pageControl.currentPage-1;
}

- (void)nextPage:(UISwipeGestureRecognizer *)swipeGestureRecognizer
{
    self.pageControl.currentPage = self.pageControl.currentPage+1;
}

- (void)pageChanged:(id)sender
{
    NSString *selectedItem = [self.items objectAtIndex:self.pageControl.currentPage];
    NSLog(@"\nSelected Item: %@\nCurrent Page: %d\nTotal Number of Pages: %d\n", selectedItem, self.pageControl.currentPage+1, self.pageControl.numberOfPages);
}

当您单击页面控件时,它会在 self.items 属性表示的字符串数组中向前或向后移动。 -pageChanged: 也是您更新视图上的控件的位置。

如果你想更新另一个区域的 UIPageControl 中的当前页面,只需执行以下操作:

self.pageControl.currentPage = 5;  // set to whatever page you need represented

UIPageControl only represents paging, it does not manage the paging behavior itself. You will need to write your own code to iterate through your array when clicking on the UIPageControl. The currentPage property should be set to the index you are working with in your array, the totalPages property should be set to the array's count property. Finally, you'll need to setup a target/action pair for the UIControlEventValueChanged event using -addTarget:action:forControlEvents:

[pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];

The documentation states:

When a user taps a page control to move to the next or previous page, the control sends the UIControlEventValueChanged event for handling by the delegate. The delegate can then evaluate the currentPage property to determine the page to display. The page control advances only one page in either direction.

So you can use that event to determine when a page is changed.

Example:

@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (strong, nonatomic) NSArray *items;

[...]

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.items = [NSArray arrayWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight",@"Nine",@"Ten", nil];
    [self.pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];

    // Left Swipe to go to previous page
    UISwipeGestureRecognizer *leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(previousPage:)];
    leftSwipeGestureRecognizer.direction = numberOfTouchesRequired = 1;
    leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:leftSwipeGestureRecognizer];

    // Right Swipe to go to next page
    UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(nextPage:)];
    rightSwipeGestureRecognizer.direction = numberOfTouchesRequired = 1;
    rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:rightSwipeGestureRecognizer];

}

- (void)previousPage:(UISwipeGestureRecognizer *)swipeGestureRecognizer
{
    self.pageControl.currentPage = self.pageControl.currentPage-1;
}

- (void)nextPage:(UISwipeGestureRecognizer *)swipeGestureRecognizer
{
    self.pageControl.currentPage = self.pageControl.currentPage+1;
}

- (void)pageChanged:(id)sender
{
    NSString *selectedItem = [self.items objectAtIndex:self.pageControl.currentPage];
    NSLog(@"\nSelected Item: %@\nCurrent Page: %d\nTotal Number of Pages: %d\n", selectedItem, self.pageControl.currentPage+1, self.pageControl.numberOfPages);
}

When you click the page control, it moves forward or backward through the array of strings represented by the self.items property. -pageChanged: is where you would update the controls on the view as well.

If you want to update the current page in the UIPageControl in another area, simply do:

self.pageControl.currentPage = 5;  // set to whatever page you need represented
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文