本文实例为大家分享了ios实现背景滑动效果的具体代码,供大家参考,具体内容如下

第一步、在很多app中,我们都可以看见那些特效绚丽的滑动选项条,那么如何才能够简单,快速的实现那样的效果呢

#import <uikit/uikit.h>  
  
@interface viewcontroller : uiviewcontroller{  
    nsmutablearray *btnarray;  
    nsmutablearray *titlearray;  
}  
  
@property (nonatomic,strong) uiview *customview;  
@property (nonatomic,strong) uiview *backview;  
@property (nonatomic,strong) uibutton *mybutton;  
  
-(void)mybuttonclcik:(id)sender;  
  
@end  

第二步:在我们的额viewdidload方法中,或者自定义一个方法中创建我么的界面元素。《这里我引日了quartzcore框架,是为了使用其layer属性》

#import "viewcontroller.h"  
#import <quartzcore/quartzcore.h>  
  
@interface viewcontroller ()  
  
@end  
  
@implementation viewcontroller  
  
@synthesize customview;  
@synthesize backview;  
@synthesize mybutton;  
  
//每行显示的button个数  
#define kselectnum 6  
  
- (void)viewdidload  
{  
    [super viewdidload];  
    // do any additional setup after loading the view, typically from a nib.  
      
    //创建背景视图,并设置背景颜色或者图片  
    customview = [[uiview alloc]initwithframe:cgrectmake(20, 100, 900, 60)];  
    customview.backgroundcolor = [uicolor blackcolor];  
    //设置customview的样式,变为圆角  
    customview.layer.cornerradius = 15.0f;  
    customview.layer.maskstobounds = yes;  
    //将customview add 到当前主view中  
    [self.view addsubview:customview];  
      
    //创建button的背景视图  
    backview = [[uiview alloc] initwithframe:cgrectmake(5, 5, 95, 50)];  
    backview.backgroundcolor = [uicolor bluecolor];  
    //设置为圆角。以免造成重叠显示  
    backview.layer.cornerradius = 15.0f;  
    backview.layer.maskstobounds = yes;  
    //将backview视图add到customview中  
    [customview addsubview:backview];  
      
      
    //创建button,首先button的个数是不固定的,因此我们需要动态的生成button  
    //创建数组,保存button的title  
    btnarray = [[nsmutablearray alloc]init];  
    titlearray = [[nsmutablearray alloc]initwithobjects:@"热播大片",@"最新更新",@"最热观看",@"美剧大片",@"韩剧频道",@"综艺娱乐", nil];  
    //动态生成button  
    for (int i = 0; i < kselectnum; i ++){  
        mybutton = [uibutton buttonwithtype:uibuttontypecustom];  
        mybutton.titlelabel.font = [uifont boldsystemfontofsize:20.0f];  
        [mybutton settitle:[titlearray objectatindex:i] forstate:uicontrolstatenormal];  
        [mybutton settitlecolor:[uicolor graycolor] forstate:uicontrolstatenormal];  
        [mybutton settitlecolor:[uicolor whitecolor] forstate:uicontrolstateselected];  
        [mybutton setframe:cgrectmake(i%(kselectnum + 1)*140+5, 5, 95, 50)];  
        [mybutton addtarget:self action:@selector(mybuttonclcik:) forcontrolevents:uicontroleventtouchupinside];  
        mybutton.tag = i;  
        [btnarray addobject:mybutton];  
        [customview addsubview:mybutton];  
          
        //设置默认选择的button.title的颜色  
        if(i == 0){  
            mybutton.selected = yes;  
        }  
    }  
}  

第三步:我们为button添加按钮点击事件,同时设置背景色滑动特效。

- (void)mybuttonclcik:(id)sender{  
//    nsstring *selectedbtn = [nsstring stringwithformat:@"%@",[titlearray objectatindex:button.tag]];  
//    uialertview *alert = [[uialertview alloc]initwithtitle:nil message:selectedbtn delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil, nil];  
//    [alert show];  
      
    //添加动画过度效果  
    [uiview beginanimations:@"slowglide" context:nil];  
    [uiview setanimationduration:0.3f];  
      
    //设置每次只能选择一个button  
    uibutton *button = (uibutton *)sender;  
    if(!button.selected){  
        for (uibutton *eachbtn in btnarray) {  
            if(eachbtn.isselected){  
                [eachbtn setselected:no];  
            }  
        }  
        [button setselected:yes];  
          
        //设置点击那个按钮,那个按钮的背景改变为backview的颜色  
        [backview setframe:button.frame];  
    }  
    [uiview commitanimations];  
}  

最后成型,我们就可以根据我们的样式需要进行调整了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。