/** * 源代码的链接 * 链接: 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]; } /** * 导航栏左边按钮的监听事件 */ - (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执行动画,所以要除以原来的比例(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按钮的监听事件 */ - (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; } /** * 创建按钮 */ - (void)creatButtons{ //创建button的个数 int count = (int)self.item.titles.count; // 通过循环创建按钮 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]; } } /** * 按钮的监听事件 */ - (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]; } //取消当前按钮选中状态 self.currentBtn.selected = NO; //设置当前按钮为选中的按钮 self.currentBtn = sender; // 设置当前按钮为选中状态 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 */
时间: 2024-10-27 19:18:38