新浪微博的簡易框架【主題選中特效】

在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






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章