網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了iOS實現(xiàn)背景滑動效果的具體代碼,供大家參考,具體內容如下
1、在很多APP中,我們都可以看見那些特效絢麗的滑動選項條,那么如何才能夠簡單,快速的實現(xiàn)那樣的效果呢
#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方法中,或者自定義一個方法中創(chuàng)建我么的界面元素。《這里我引日了QuartzCore框架,是為了使用其layer屬性》
#import "ViewController.h" ? #import? ?? @interface ViewController () ? ?? @end ? ?? @implementation ViewController ? ?? @synthesize customView; ? @synthesize backView; ? @synthesize myButton; ? ?? //每行顯示的button個數(shù) ? #define kSelectNum 6 ? ?? - (void)viewDidLoad ? { ? ? ? [super viewDidLoad]; ? ? ? // Do any additional setup after loading the view, typically from a nib. ? ? ? ?? ? ? //創(chuàng)建背景視圖,并設置背景顏色或者圖片 ? ? ? customView = [[UIView alloc]initWithFrame:CGRectMake(20, 100, 900, 60)]; ? ? ? customView.backgroundColor = [UIColor blackColor]; ? ? ? //設置customView的樣式,變?yōu)閳A角 ? ? ? customView.layer.cornerRadius = 15.0f; ? ? ? customView.layer.masksToBounds = YES; ? ? ? //將customView add 到當前主View中 ? ? ? [self.view addSubview:customView]; ? ? ? ?? ? ? //創(chuàng)建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]; ? ? ? ?? ? ? ?? ? ? //創(chuàng)建button,首先button的個數(shù)是不固定的,因此我們需要動態(tài)的生成button ? ? ? //創(chuàng)建數(shù)組,保存button的title ? ? ? btnArray = [[NSMutableArray alloc]init]; ? ? ? titleArray = [[NSMutableArray alloc]initWithObjects:@"熱播大片",@"最新更新",@"最熱觀看",@"美劇大片",@"韓劇頻道",@"綜藝娛樂", nil]; ? ? ? //動態(tài)生成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; ? ? ? ? ? } ? ? ? } ? } ?
第三步:我們?yōu)閎utton添加按鈕點擊事件,同時設置背景色滑動特效。
- (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]; ? ? ? ? ? ?? ? ? ? ? //設置點擊那個按鈕,那個按鈕的背景改變?yōu)閎ackView的顏色 ? ? ? ? ? [backView setFrame:button.frame]; ? ? ? } ? ? ? [UIView commitAnimations]; ? } ?
最后成型,我們就可以根據(jù)我們的樣式需要進行調整了。
原文鏈接:https://blog.csdn.net/yihaoxue/article/details/84295290
相關推薦
- 2022-04-20 python錯誤提示:Errno?2]?No?such?file?or?directory的解決方法
- 2023-03-23 Pandas時間數(shù)據(jù)處理詳細教程_python
- 2022-05-14 Python實現(xiàn)簡單的圖書管理系統(tǒng)_python
- 2022-04-17 C語言中用棧+隊列實現(xiàn)隊列中的元素逆置_C 語言
- 2023-02-12 python中使用docx模塊處理word文檔_python
- 2023-11-22 python使用matplotlib.pyplot畫不了圖
- 2021-12-10 Android?Activity活動頁面跳轉與頁面?zhèn)髦礯Android
- 2022-08-10 go?modules中replace使用方法_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支