0
我在UIScrollView內部的任何地方從1到300個按鈕動態插入(作爲子視圖)。目前,按鈕排列很好,但不會按照我希望的那樣滾動。它只是水平滾動而沒有分頁效果。理想情況下,他們的方式,我想它的工作是:我怎樣才能使這個工作與UIScrollView?
- 顯示每「頁」
- 當向右滾動,顯示下一個「頁」 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];
}
我想我添加的按鈕將不允許米路e實施我正在努力完成的任務。你們認爲我在這裏做錯了什麼?
我可以通過沒有問題的滾動,但它不表現我想要的方式。目前它從左到右滾動,但它一次不顯示48個按鈕(我的意思是,如果我滾動到右側,我應該看到一組全新的48個按鈕) – MrShoot
我很抱歉我只是在愚蠢:(pagingEnabled爲我做了。現在我只需要調整尺寸,以便按鈕的邊緣不會顯示在新頁面上 – MrShoot