gpt4 book ai didi

ios - 我怎样才能使这个与 UIScrollView 一起工作?

转载 作者:行者123 更新时间:2023-11-29 04:59:40 25 4
gpt4 key购买 nike

我在 UIScrollView 内的任何位置动态插入 1 到 300 个按钮(作为 subview )。目前,按钮排列得很好,但没有像我希望的那样滚动。它只是水平滚动,没有分页效果。理想情况下,我希望它的工作方式是:

  • 每个“页面”最多显示 48 个按钮
  • 向右滚动时,显示下一个“页面”以及 48 个附加按钮

我的代码:

-(void)populateScrollView:(NSMutableArray *)colors {
// Properties
NSInteger count = 0;
NSInteger rowsize = 48;
CGFloat pageNum = 6;
CGFloat pageWidth = scrollView.frame.size.width;
CGFloat pageHeight = scrollView.frame.size.height;
CGFloat visibleWidth = 465;

myButtons = [[NSMutableArray alloc]init];

// Clear the subviews if any
for (UIView *subview in scrollView.subviews) {
[subview removeFromSuperview];
}

for (Color *element in colors) {
// Set the button properties
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [UIImage imageNamed:@"color_overlay.png"];
[button setImage:image forState:UIControlStateNormal];
[button setImage:image forState:UIControlStateHighlighted];
div_t temp = div(count, rowsize);
button.frame = CGRectMake(52 * temp.rem , 52*temp.quot, 52, 52);
button.tag = count++;
UIImage *effect = [UIImage imageNamed:element.image_resource];
effectImage = [[UIImageView alloc] initWithImage:effect];
effectImage.frame = CGRectMake(52 * temp.rem , 52*temp.quot, 52, 52);

// Color the graphic if color available. If not, load the image
unsigned result = 0;
if ([element.hex_value length] > 1){
NSScanner *scanner = [NSScanner scannerWithString:element.hex_value];
[scanner scanHexInt:&result];
button.backgroundColor = UIColorFromRGB(result);
}
else {
effectImage.image = effect;
}
// Set actions for the color
[button addTarget:self action:@selector(changeGraphicColor:) forControlEvents:UIControlEventTouchUpInside];
[myButtons addObject:button];

for (UIButton *myButton in myButtons){
[scrollView insertSubview:effectImage atIndex:1];
[scrollView insertSubview:myButton atIndex:2];
}
}
// Set scrollView contentSize and create pages:
CGFloat leftShift=floor(((visibleWidth-pageWidth)/2)/pageWidth)*pageWidth;
CGFloat contentSizeReduction=2*leftShift;

scrollView.contentSize = CGSizeMake(pageNum*pageWidth-contentSizeReduction,pageHeight);

[myButtons removeAllObjects];
[myButtons release];

}

我认为添加按钮的方式不允许我实现我想要完成的任务。你们认为我在这里做错了什么?

最佳答案

您是否将scrollView.pagingEnabled设置为YES?

关于ios - 我怎样才能使这个与 UIScrollView 一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7258259/

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