做这个:
将这些添加到 .m 文件的标题中
#define viewWidth 40 //button width
#define viewHeight 30 //button height
#define viewOffsetX 5 //button left ie X
#define viewOffsetY 5 //button right ie Y
#define viewXspace 15 //button from top X space
#define ViewYspace 5 //button from bottom
你有按钮的图像将它们添加到数组中,即 arrBtnImages
现在使用这些方法在您的滚动视图中加载按钮,即 viewDidLoad 方法中的 scView
- (void)loadBtnInSlider
{
int row = 1;
int col = [arrBtnImages count] / row;
if ([mut_arrImages count] % col != 0 ) {
col++;
}
int index = 0;
for (int i=0; i < row ; i++)
{
CGRect frame;
frame.size = CGSizeMake(viewWidth, viewHeight);
frame.origin.y = (i * viewHeight) + (i * ViewYspace) + viewOffsetY;
for (int j= 0; j < col && index < [mut_arrImages count]; j++) {
CGRect frame;
frame.size = CGSizeMake(viewWidth, viewHeight);
frame.origin.x = (j * viewWidth) + (j * viewXspace) + viewOffsetX;
frame.origin.y = viewOffsetY;
UIButton *btn = [[UIButton alloc]initWithFrame:frame];
[btn setTag:j];
[btn addTarget:self action:@selector(btnSelector:) forControlEvents:UIControlEventTouchUpInside];
[btn setUserInteractionEnabled:YES];
[btn setImage:[UIImage imageNamed:[arrBtnImages objectAtIndex:j]] forState:UIControlStateNormal];
index++;
[scView addSubview:btn];
[btn release];
}
}
[scView setContentSize:CGSizeMake(col * (viewWidth+ViewYspace)+viewOffsetY,scView.frame.size.height)];
}
在 vieDidLoad 中使用此方法,如下所示:
[self loadBtnInSlider];
现在在 .h 文件中为按钮添加下面的选择器方法,它会是这样的
-(void)btnSelector:(id)sender
{
UIButton *btnSelected = sender;
switch(btnSelected.tag)
{
// add case as much u have button
case 0:
//first button called
break;
case 1:
//second button called
break;
::
::
}
}