仿网易抽屉效果
/*** 源代碼的鏈接* 鏈接: http://pan.baidu.com/s/1c2hqDzy 密碼: jscx*/#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@end #import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// Override point for customization after application launch.return YES;
}@end #import <UIKit/UIKit.h>@interface ViewController : UIViewController@end #import "ViewController.h"
#import "LFCustomButton.h"
#import "LFLeftView.h"
#import "LFItems.h"
#import "LFSmileViewController.h"
#import "LFCheerViewController.h"
#import "LFHappyViewController.h"const float leftViewTopGap = 120;
const float viewAnimateDuration = 0.3;
@interface ViewController ()<LFLeftViewDelegate>@property (nonatomic , strong) UIImageView *bgView;//背景圖片
@property (nonatomic , strong) LFLeftView *leftView;//左邊的菜單欄
@property (nonatomic , strong) UIButton *coverBtn;//防止內容被操作的按鈕
@property (nonatomic , strong) UINavigationController *currentVC;//當前控制器@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// controllers = [NSMutableArray array];// 設置背景圖片self.bgView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];self.bgView.backgroundColor = [UIColor blackColor];self.bgView.opaque = 0.3;// 設置可操作(如果在此不設置可操作,它的子視圖是不可以操作的)self.bgView.userInteractionEnabled = YES;[self.view addSubview:self.bgView];LFItems *item = [[LFItems alloc] init];//圖片名item.images = @[@"De",@"Dx",@"monkey"];//標題的名稱item.titles = @[@"開心",@"快樂",@"幸福"];CGPoint point= CGPointMake(0, leftViewTopGap);self.leftView = [[LFLeftView alloc] initWithItem:item originPoint:point];self.leftView.delegate = self;[self.bgView addSubview:self.leftView];LFSmileViewController *smileVC = [[LFSmileViewController alloc] init];[self initVC:smileVC title:@"開心"];LFCheerViewController *cheerVC = [[LFCheerViewController alloc] init];[self initVC:cheerVC title:@"快樂"];LFHappyViewController *happyVC = [[LFHappyViewController alloc] init];[self initVC:happyVC title:@"幸福"];// 默認第一個控制器為當前控制器self.currentVC = self.childViewControllers[0];[self.bgView addSubview:self.currentVC.view];
}/*** 初始化導航控制器** @param vc 控制器* @param title 導航欄的標題*/
- (void)initVC:(UIViewController *)vc title:(NSString *)title{UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vc];UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"左菜單欄" style:UIBarButtonItemStylePlain target:self action:@selector(showLeftMenuBar)];vc.title = title;vc.navigationItem.leftBarButtonItem = leftItem;[self addChildViewController:navi];[navi.view removeFromSuperview];
}/*** 導航欄左邊按鈕的監(jiān)聽事件*/
- (void)showLeftMenuBar{NSLog(@"顯示左菜單欄");[self animation];
}/*** 縮小平移動畫*/
- (void)animation{[UIView animateWithDuration:viewAnimateDuration animations:^{//計算縮放的比例float widthScale = 1 - self.leftView.frame.size.width/(UISCREEN_WIDTH*1.0);float heightScale = 1 - (leftViewTopGap * 2)/(UISCREEN_HEIGHT *1.0);//縮放CGAffineTransform scaleTransForm = CGAffineTransformMakeScale(widthScale, heightScale);//計算平移的距離CGFloat distanceX = UISCREEN_WIDTH * (1-widthScale)*0.5;//平移(因為moveTransForm是基于scaleTransForm執(zhí)行動畫,所以要除以原來的比例(widthScale))CGAffineTransform moveTransForm = CGAffineTransformTranslate(scaleTransForm, distanceX/widthScale, 0);self.currentVC.view.transform = moveTransForm;} completion:^(BOOL finished) {self.coverBtn = [UIButton buttonWithType:UIButtonTypeCustom];self.coverBtn.frame = self.currentVC.view.bounds;self.coverBtn.backgroundColor = [UIColor clearColor];[self.coverBtn addTarget:self action:@selector(coverbtnAction:) forControlEvents:UIControlEventTouchUpInside];[self.currentVC.view addSubview:self.coverBtn];}];}/*** coverBtn按鈕的監(jiān)聽事件*/
- (void)coverbtnAction:(UIButton *)sender{[self recoverAnimation];
}/*** 恢復原來尺寸的動畫*/
- (void)recoverAnimation{[UIView animateWithDuration:viewAnimateDuration animations:^{self.currentVC.view.transform =CGAffineTransformIdentity;} completion:^(BOOL finished) {[self.coverBtn removeFromSuperview];}];
}#pragma mark -- LFLeftViewDelegate --
- (void)exchangeControllerFromIndex:(int)index toIndex:(int)nextIndex{//移除舊控制器
[self.currentVC.view removeFromSuperview];[self recoverAnimation];//顯示新的控制器UINavigationController *willShowVC = self.childViewControllers[nextIndex];self.currentVC = willShowVC;[self.bgView addSubview:willShowVC.view];}@end #import <UIKit/UIKit.h>@interface LFSmileViewController : UIViewController@end #import "LFSmileViewController.h"@interface LFSmileViewController ()@end@implementation LFSmileViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor redColor];
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}@end #import <UIKit/UIKit.h>@interface LFCheerViewController : UIViewController@end #import "LFCheerViewController.h"@interface LFCheerViewController ()@end@implementation LFCheerViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor orangeColor];
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end #import <UIKit/UIKit.h>@interface LFHappyViewController : UIViewController@end #import "LFHappyViewController.h"@interface LFHappyViewController ()@end@implementation LFHappyViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor yellowColor];
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end #import <Foundation/Foundation.h>@interface LFItems : NSObject/*** images存放圖片名*/
@property (nonatomic , strong) NSArray *images;/*** titles 名稱*/
@property (nonatomic, strong) NSArray *titles;@end #import "LFItems.h"@implementation LFItems/*** 初始化時,給images和titles申請內存*/
- (instancetype)init
{self = [super init];if (self) {self.images = [[NSArray alloc] init];self.titles = [[NSArray alloc] init];}return self;
}@end #import <UIKit/UIKit.h>@interface LFCustomButton : UIButton@end #import "LFCustomButton.h"const float LFCustomButtonScale = 0.5;@implementation LFCustomButton/*** 重寫按鈕title的尺寸*/
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{return CGRectMake(contentRect.size.width * LFCustomButtonScale, 0, contentRect.size.width *(1 - LFCustomButtonScale), contentRect.size.height);
}/*** 重寫按鈕Image的尺寸*/
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{CGRect titleFrame = CGRectMake(0, 0, contentRect.size.width * LFCustomButtonScale, contentRect.size.height);return titleFrame;
}@end #import <UIKit/UIKit.h>@class LFItems;
@protocol LFLeftViewDelegate;@interface LFLeftView : UIView@property (nonatomic , weak) id<LFLeftViewDelegate> delegate;/*** 初始化的方法** @param item 數據(title和image)* @param point 在父視圖的初始位置** @return 返回LFLeftView的對象*/
- (LFLeftView *)initWithItem:(LFItems *)item originPoint:(CGPoint)point;@end@protocol LFLeftViewDelegate <NSObject>- (void)exchangeControllerFromIndex:(int)index toIndex:(int)nextIndex;@end #import "LFLeftView.h"
#import "LFItems.h"
#import "LFCustomButton.h"
#import "UIImage+CreatImageWithColor.h"
#define LFCustomButtonWidth UISCREEN_WIDTH/2.0
#define LFCustomButtonHeight 60@interface LFLeftView()@property (nonatomic, strong) LFItems *item;@property (nonatomic, strong) NSMutableArray *buttons;@property (nonatomic, strong) UIButton *currentBtn;//當前選中的按鈕@end@implementation LFLeftView- (LFLeftView *)initWithItem:(LFItems *)item originPoint:(CGPoint)point
{self = [super init];if (self) {if (item == nil) {return nil;}self.buttons = [NSMutableArray array];self.userInteractionEnabled = YES;self.item = item;self.frame = CGRectMake(point.x, point.y, LFCustomButtonWidth, LFCustomButtonHeight * self.item.titles.count);[self creatButtons];}return self;
}/*** 創(chuàng)建按鈕*/
- (void)creatButtons{//創(chuàng)建button的個數int count = (int)self.item.titles.count;// 通過循環(huán)創(chuàng)建按鈕for (int i = 0; i < count; i++) {LFCustomButton *button = [LFCustomButton buttonWithType:UIButtonTypeCustom];//設置選中時的背景圖片[button setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:253/255.0 green:0 blue:0 alpha:0.2]] forState:UIControlStateSelected];//默認第一個按鈕為當前選中按鈕if (i == 0) {self.currentBtn = button;self.currentBtn.selected = YES;}button.backgroundColor = [UIColor grayColor];[button setTitle:self.item.titles[i] forState:0];if (self.item.images[i] != nil) {[button setImage:[UIImage imageNamed:self.item.images[i]] forState:0];}button.tag = i;[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];[self addSubview:button];[self.buttons addObject:button];}
}/*** 按鈕的監(jiān)聽事件*/
- (void)buttonAction:(UIButton *)sender{NSLog(@"從第%ld個按鈕跳到第%ld個按鈕",(long)self.currentBtn.tag,(long)sender.tag);if ([_delegate respondsToSelector:@selector(exchangeControllerFromIndex: toIndex:)]) {[_delegate exchangeControllerFromIndex:(int)self.currentBtn.tag toIndex:(int)sender.tag];}//取消當前按鈕選中狀態(tài)self.currentBtn.selected = NO;//設置當前按鈕為選中的按鈕self.currentBtn = sender;// 設置當前按鈕為選中狀態(tài)self.currentBtn.selected = YES;
}- (void)layoutSubviews{[super layoutSubviews];// 設置所有子視圖的尺寸for (int i = 0; i < self.buttons.count; i++) {UIButton *button = self.buttons[i];button.frame = CGRectMake(0, i*LFCustomButtonHeight, LFCustomButtonWidth, LFCustomButtonHeight);}
}@end #import <UIKit/UIKit.h>@interface UIImage (CreatImageWithColor)/*** 通過顏色生成該純顏色的圖片** @param color 生成圖片的顏色** @return 返回圖片*/
+ (UIImage *)imageWithColor:(UIColor *)color;/*** 通過顏色生成該純顏色的圖片** @param color 生成圖片的顏色* @param width 生成圖片的寬* @param height 生成圖片的高** @return 返回圖片*/
+ (UIImage *)imageWithColor:(UIColor *)color imageWidth:(CGFloat)width imageWithHeight:(CGFloat)height;@end #import "UIImage+CreatImageWithColor.h"@implementation UIImage (CreatImageWithColor)+ (UIImage *)imageWithColor:(UIColor *)color{return [UIImage imageWithColor:color imageWidth:100 imageWithHeight:100];
}+ (UIImage *)imageWithColor:(UIColor *)color imageWidth:(CGFloat)width imageWithHeight:(CGFloat)height{//開啟基于位圖的圖形上下文UIGraphicsBeginImageContextWithOptions(CGSizeMake(width,height), NO, 0.0);// 設置畫筆的顏色[color set];// 畫矩形,并填充UIRectFill(CGRectMake(0, 0, width, height));//獲取圖片UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();//關閉上下文
UIGraphicsEndImageContext();return resultImage;
}@end #ifndef PrefixHeader_pch
#define PrefixHeader_pch// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.#define UISCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define UISCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height#endif /* PrefixHeader_pch */
?
轉載于:https://www.cnblogs.com/lantu1989/p/5462623.html
總結
- 上一篇: SpringMVC拦截器2(资源和权限管
- 下一篇: Scrum项目1.0