我创建了一个 UIScrollView,其分页包含多个图像,每个“页面”有 1 个图像,我想知道是否有任何方法可以区分这些图像? 外汇。当显示第一张图片时,我会有一个按钮将 UILabel 设置为“1”,而当显示第二张图片时,同一个按钮会将相同的 UILabel 设置为“2”。
这是我的代码:
pictures = [NSArray arrayWithObjects: [UIImage imageNamed:@"AND.png"], [UIImage imageNamed:@"ALB.png"], nil];
for (int i = 0; i < pictures.count; i++) {
CGRect frame;
frame.origin.x = 135 + (self.scrollViewQuiz.frame.size.width * i);
frame.origin.y = 40;
frame.size = CGSizeMake(50, 50);
UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
subview.image = [pictures objectAtIndex:i];
[self.scrollViewQuiz addSubview:subview];
}
self.scrollViewQuiz.contentSize = CGSizeMake(self.scrollViewQuiz.frame.size.width * pictures.count, self.scrollViewQuiz.frame.size.height);
最佳答案
听起来您只想计算 ScrollView 当前显示的“页码”。您可以尝试像这样作为按钮的操作:
- (IBAction)buttonWasTapped:(id)sender {
UIScrollView *scrollView = self.scrollViewQuiz;
int pageNumber = (int)roundf(scrollView.contentOffset.x / scrollView.bounds.size.width);
self.label.text = [NSString stringWithFormat:@"%d", pageNumber];
}
关于objective-c - UIScrollView 分页标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11066812/