gpt4 book ai didi

ios - 启用分页的 scrollView 无法正常工作? ( objective-C )

转载 作者:行者123 更新时间:2023-11-29 01:22:13 25 4
gpt4 key购买 nike

我想用三个 UIView 创建分页 scrollView。而不是想将 imageView 作为 subview 添加到这些 UIView 中。我无法弄清楚如何计算 imageViews 的框架。当我在没有 imageViews 的情况下运行代码时,它工作得很好。

- (void)viewDidLoad
{
[super viewDidLoad];

self.scrollView.delegate = self;
self.scrollView.frame = self.view.frame;

NSArray *colorsArray = [NSArray arrayWithObjects:[UIColor blueColor], [UIColor yellowColor],[UIColor greenColor], nil];

NSArray *imagesArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"123.png"],[UIImage imageNamed:@"13.png"],[UIImage imageNamed:@"12.png"],nil];

for (int i = 0; i < colorsArray.count; i++) {
CGRect frame;
frame.origin.x = self.view.frame.size.width * i;
frame.size = self.view.frame.size;

self.scrollView.pagingEnabled = YES;

UIView *view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [colorsArray objectAtIndex:i];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:view.frame];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [imagesArray objectAtIndex:i];
[view addSubview:imageView];

[self.scrollView addSubview:view];

}

self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * colorsArray.count, self.scrollView.frame.size.height);

self.pageControl.numberOfPages = 3;
self.pageControl.currentPage = 0;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat pageWidth = CGRectGetWidth(self.scrollView.bounds);
CGFloat pageFraction = self.scrollView.contentOffset.x / pageWidth;
self.pageControl.currentPage = roundf(pageFraction);
}

最佳答案

ImageView 的框架需要与其父 View (即 View )相关,而不是 ScrollView 。要实现你想要的分页改变:

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:view.frame];

到:

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, view.frame.size.width, view.frame.size.height)];

关于ios - 启用分页的 scrollView 无法正常工作? ( objective-C ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34465691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com