這篇文章主要介紹了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]; }
最后成型,我們就可以根據我們的樣式需要進行調整了。
感謝你能夠認真閱讀完這篇文章,希望小編分享的“iOS如何實現背景滑動效果”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。