首页 > 代码库 > 新浪微博的简易框架【主题选中特效】
新浪微博的简易框架【主题选中特效】
在AppDelegate.m中设置根视图控制器
RootviewController.h
@interface RootTabbarController : UITabBarController { UIImageView *_selectedImg; }RootviewController.m
#import "RootTabbarController.h" #import "HomeViewController.h" #import "MessageViewController.h" #import "PersonalViewController.h" #import "DiscoverViewController.h" #import "MoreViewController.h" @interface RootTabbarController () @end @implementation RootTabbarController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; //创建三级控制器 [self _initViewCtrls]; //自定义标签视图 [self _initTabbarView]; } //创建三级控制器 - (void)_initViewCtrls { //1.创建一级控制器 HomeViewController *homeCtrl = [[HomeViewController alloc] init]; MessageViewController *messageCtrl = [[MessageViewController alloc] init]; PersonalViewController *personalCtrl = [[PersonalViewController alloc] init]; DiscoverViewController *discoverCtrl = [[DiscoverViewController alloc] init]; MoreViewController *moreCtrl = [[MoreViewController alloc] init]; NSArray *viewCtrls = @[homeCtrl,messageCtrl,personalCtrl,discoverCtrl,moreCtrl]; //用于存放导航控制器 NSMutableArray *navCtrls = [[NSMutableArray alloc] initWithCapacity:viewCtrls.count]; //2.将视图控制器交给导航控制器控制 for (int i=0; i<viewCtrls.count; i++) { //取出视图控制器 UIViewController *viewCtrl = viewCtrls[i]; UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:viewCtrl]; [navCtrls addObject:navCtrl]; } //3.创建三级控制器 self.viewControllers = navCtrls; } //自定义标签视图 - (void)_initTabbarView { //1.移除tabbar上面的按钮 NSArray *subView = [self.tabBar subviews]; for (UIView *view in subView) { Class cla = NSClassFromString(@"UITabBarButton"); if ([view isKindOfClass:cla]) { [view removeFromSuperview]; } } //2.设置背景视图 [self.tabBar setBackgroundImage:[UIImage imageNamed:@"mask_navbar"]]; //3.添加按钮 for (int i=0; i<5; i++) { NSString *name = [NSString stringWithFormat:@"home_tab_icon_%d",i+1]; //创建按钮 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:[UIImage imageNamed:name] forState:UIControlStateNormal]; button.tag = i; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(64*i, (49-45)/2, 64, 45); [self.tabBar addSubview:button]; } //4.添加选择按钮 _selectedImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 64, 45)]; _selectedImg.image = [UIImage imageNamed:@"home_bottom_tab_arrow"]; _selectedImg.backgroundColor = [UIColor clearColor]; [self.tabBar addSubview:_selectedImg]; } - (void)buttonAction:(UIButton *)button { //点击按钮切换试图 self.selectedIndex = button.tag; //设置点击高亮的效果 button.showsTouchWhenHighlighted = YES; //点击后选择的滑动效果 [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:.35]; _selectedImg.center = button.center; [UIView commitAnimations]; } @end
HomeViewController.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = @"首页"; } return self; } - (void)viewDidLoad { [super viewDidLoad]; //创建左侧的按钮 UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom]; leftButton.frame = CGRectMake(0, 0, 130, 43); //设置背景图片 UIImage *image = [UIImage imageNamed:@"button_title"]; //设置拉伸点 image = [image stretchableImageWithLeftCapWidth:50 topCapHeight:0]; [leftButton setBackgroundImage:image forState:UIControlStateNormal]; //创建一个image UIImageView *buttonImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 0, 44, 44)]; buttonImageView.image = [UIImage imageNamed:@"button_icon_group"]; buttonImageView.backgroundColor = [UIColor clearColor]; [leftButton addSubview:buttonImageView]; //创建一个label UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 0, 80, 44)]; label.backgroundColor = [UIColor clearColor]; label.text = @"我的微博"; label.font = [UIFont boldSystemFontOfSize:17]; label.textColor = [UIColor whiteColor]; [leftButton addSubview:label]; UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton]; self.navigationItem.leftBarButtonItem = leftItem; //创建右侧的按钮 UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; rightButton.frame = CGRectMake(0, 0, 43, 43); //创建背景图片 UIImage *bkImage = [UIImage imageNamed:@"button_m"]; [rightButton setBackgroundImage:bkImage forState:UIControlStateNormal]; [rightButton setImage:[UIImage imageNamed:@"button_icon_plus"] forState:UIControlStateNormal]; UIBarButtonItem *rightitem = [[UIBarButtonItem alloc] initWithCustomView:rightButton]; self.navigationItem.rightBarButtonItem = rightitem; }
MessageViewController.m
PersonalViewController.m
DiscoverViewController.m
MoreViewController.m
#import "MoreViewController.h" #import "ThemeViewController.h" @interface MoreViewController () @end @implementation MoreViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = @"更多"; } return self; } - (void)viewDidLoad { [super viewDidLoad]; //设置导航栏不穿透 self.edgesForExtendedLayout = UIRectEdgeNone; //创建主题按钮 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(10, 30, 300, 50); button.backgroundColor = [UIColor blueColor]; [button setTitle:@"我的主题" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } - (void)buttonAction { ThemeViewController *themeCtrl = [[ThemeViewController alloc] init]; [self.navigationController pushViewController:themeCtrl animated:YES]; }ThemeViewController.h
@interface ThemeViewController : UIViewController { UIImageView *_checkimg; }
ThemeViewController.m
#import "ThemeViewController.h" @interface ThemeViewController () @end @implementation ThemeViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization <span style="color:#cc0000;">self.hidesBottomBarWhenPushed = YES;</span> } return self; } - (void)viewDidLoad { [super viewDidLoad]; UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backcolor"]]; self.view.backgroundColor = color; //创建子视图上侧的视图 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 106)]; imageView.image = [UIImage imageNamed:@"topbg"]; [self.view addSubview:imageView]; //创建子视图 for (int i=0; i<8; i++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; NSString *name = [NSString stringWithFormat:@"%d",i+1]; [button setImage:[UIImage imageNamed:name] forState:UIControlStateNormal]; int row = i/2; if (i%2 == 0) { //偶数 button.frame = CGRectMake(5, 130+(62+5)*row, 150, 62); } else { //奇数 button.frame = CGRectMake(165, 130+(62+5)*row, 150, 62); } button.tag = i+100; //添加点击事件 [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } //通过tag去button UIButton *button = (UIButton *)[self.view viewWithTag:100]; //创建选中标识图片 _checkimg = [[UIImageView alloc] initWithFrame:CGRectMake(130+button.frame.origin.x, 40+button.frame.origin.y, 18, 18)]; _checkimg.image = [UIImage imageNamed:@"checkmark"]; [self.view addSubview:_checkimg]; } - (void)viewWillAppear:(BOOL)animated { self.edgesForExtendedLayout = UIRectEdgeNone; [self.navigationController.navigationBar setBarStyle:UIBarStyleBlack]; [super viewWillAppear:animated]; } - (void)viewWillDisappear:(BOOL)animated { [self.navigationController.navigationBar setBarStyle:UIBarStyleDefault]; [super viewWillDisappear:animated]; } - (void)buttonAction:(UIButton *)button { <span style="color:#cc0000;"> _checkimg.frame = CGRectMake(130+button.frame.origin.x, 40+button.frame.origin.y, 18, 18);</span> } @end
新浪微博的简易框架【主题选中特效】
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。