我有许多图像按钮的滚动视图,对于按钮 1-8,按钮行为没有问题。它可以是可点击的日志。
问题是第二页上的按钮看起来不像在选择器上听(按钮 9-10)。页面控制可能有问题吗?或者我在这里错过了什么?
// button paging
myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"img_1.png"],
[UIImage imageNamed:@"img_2png"],
[UIImage imageNamed:@"img_3.png"],
[UIImage imageNamed:@"img_4.png"],
[UIImage imageNamed:@"img_5.png"],
[UIImage imageNamed:@"img_6.png"],
[UIImage imageNamed:@"img_7.png"],
[UIImage imageNamed:@"img_8.png"],
[UIImage imageNamed:@"img_9.png"],
[UIImage imageNamed:@"img_10.png"],
nil
];
// prepare counter
int posX, posY = 5, counter = 0, pages = 0;
BOOL reset = NO;
// uiviewscroller
CGRect scrollFrame;
scrollFrame.origin.x = self.myScrollView.frame.size.width * pages;
scrollFrame.origin.y = 0;
scrollFrame.size = self.myScrollView.frame.size;
UIView *subView = [[UIView alloc] initWithFrame:scrollFrame];
subView.backgroundColor = [UIColor colorWithRed:232.0f/255.0f green:233.0f/255.0f blue:235.0f/255.0f alpha: 1];
// loop over images
for (int i = 0; i < myImages.count; i++)
{
counter++;
// reposition X
if (counter == 1 || counter == 5)
{
posX = 4;
// more than 1 page
if (pages > 0)
posX = 320 * pages;
}
else
{
posX += 80;
}
// reposition Y
posY = (counter <= 4) ? 5 : 85;
if (reset == YES)
{
// reflag
reset = NO;
// redefined size
scrollFrame.origin.x = self.myScrollView.frame.size.width * pages;
}
// create image view for button
UIImage *myIcon = [myImages objectAtIndex:i];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
// manipulate button behavior
myButton.frame = CGRectMake(posX, posY, 80, 80);
[myButton setTitle:[NSString stringWithFormat:@"%d", i] forState:UIControlStateNormal];
[myButton setImage:myIcon forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
// add image to subview
[subView addSubview:myButton];
if (counter == 8)
{
// reset position
posX = 4;
posY = 5;
// populate counter
counter = 0;
pages++;
// reset section
reset = YES;
// add subview to view
[self.myScrollView addSubview:subView];
}
// add balance subview to scroller
if (counter > 0 && i == myImages.count-1)
{
// add subview to view
[self.myScrollView addSubview:subView];
pages++;
}
}
scrollFrame.origin.x = self.myScrollView.frame.size.width * pages;
self.myScrollView.contentSize = CGSizeMake(self.myScrollView.frame.size.width * pages, self.myScrollView.frame.size.height);
self.myPaging.numberOfPages = pages;