如何在iPhone中制作UIButton垂直Silder菜单

如何在iPhone中制作UIButton垂直Silder菜单

本文介绍了如何在iPhone中制作UIButton垂直Silder菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想使用滚动视图制作UIButton滑块,当我们滚动按钮的uiscrollview时,该按钮将位于中心,请参阅这些应用程序的第一个屏幕 http://itunes.apple.com/au/app/id422249255?mt=8 我该怎么办?我目前可以使用的方法我正在使用UIScrollview的这些委托方法

In my application i want to make UIButton slider with scroll view in which when we scroll uiscrollview of buttons then the button will be locate at the center please see these application's first screen sot http://itunes.apple.com/au/app/id422249255?mt=8 how can i do it and what methods i can used for it currently i am used these delegate methods of UIScrollview

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


}

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

}

(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)aScrollView
{

}

推荐答案

执行以下操作:

将它们添加到.m文件的标题中

Add these in header of .m file

#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

You have images of button add them in array ie arrBtnImages

现在使用这些方法在您的scrollView中加载按钮,即在viewDidLoad方法中加载scView

Now use these method to load button in your scrollView ie scView in viewDidLoad method

- (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中使用此方法:

Use this method in vieDidLoad like this:

[self loadBtnInSlider];

现在在.h文件中为按钮添加下面的选择器方法,就像这样

Now add below selector method for button in .h file and it would be like this

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

这篇关于如何在iPhone中制作UIButton垂直Silder菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:26