iOS如何实现背景滑动效果
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章主要介绍了iOS如何实现背景滑动效果,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。具体内容如下第一步、在很多APP中,我们都可
千家信息网最后更新 2026年11月03日iOS如何实现背景滑动效果
这篇文章主要介绍了iOS如何实现背景滑动效果,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
具体内容如下
第一步、在很多APP中,我们都可以看见那些特效绚丽的滑动选项条,那么如何才能够简单,快速的实现那样的效果呢
#import@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@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]; }最后成型,我们就可以根据我们的样式需要进行调整了。
感谢你能够认真阅读完这篇文章,希望小编分享的"iOS如何实现背景滑动效果"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!
背景
效果
篇文章
按钮
视图
颜色
个数
动态
同时
圆角
大片
方法
样式
特效
频道
生成
选择
绚丽
事件
价值
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
花魁咖啡
广州百单互联网科技有限公司
未来的互联网科技
深圳齐家互联网科技
赫美联合互联网科技
融贸通互联网科技
深圳卡尔迅互联网科技有限公司怎么样
成都玖崟互联网科技有限公司乐商圈怎么样
互联网科技中介
茂名市互联网科技产业园
互联网金融科技公司干啥的
广州恒生互联网科技有限
武汉健翔互联网络科技有限公司
智腾互联网科技有限公司
搜狐科技互联网
工业互联网山东科技大学
抖集互联网科技技术咨询
银川新华互联网科技学校原画
盐城市攀科互联网科技有限公司
互联网金融金融科技
圆梦互联网科技
金融科技 互联网金融
互联网金融 科技金融
深圳天众互联网科技公司
广西拼得多互联网科技有限公司
哈尔滨新华互联网科技学院
银行科技岗属于互联网吗
福建互联网展示科技馆设计
互联网科技企业商标注意事项
保山浩联互联网科技有限公司