如何在iPhone上制作UIButton垂直滑动菜单

3

我想在我的应用程序中创建一个带有滑动视图的UIButton滑块,在滚动UIButton的UIScrollView时,按钮会位于中心位置。请查看这些应用程序的第一个屏幕截图 http://itunes.apple.com/au/app/id422249255?mt=8。我应该如何实现它?我可以使用哪些方法?目前,我正在使用UIScrollView的以下代理方法:

-(void)scrollViewDidScroll:(UIScrollView *)aScrollView
{


}

(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{

}

(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)aScrollView
{

}
1个回答

1
做这个:
在.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方法中加载scrollView中的按钮,即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)];
}

在viewDidLoad中使用以下方法:

[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;
      ::
      ::
   }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接