[IOS]新浪微博项目2.0

来源:互联网 发布:税务总局网络培训学院 编辑:程序博客网 时间:2024/06/06 02:51


上一章展示了,此项目的大纲及及运行效果,从本章开始,我们开始新浪微博项目的具体实现


首先我们创建一个项目,因为IOS的不断更新,我们采用ARC的自动内存管理。

1.项目创建好之后,需要手动在文件夹中创建几个文件夹,以便于项目的后续之作

2.需要创建一个公共Base类,和五个标签控制器,每个标签都采用MVC的架构模式,如下:


3.搭建界面

①、项目整体分为五个模块,首页,消息,个人中心,广场,更多

②、创建

UINavigationController 和 

UITabBarController 作为五个子模块的管理器,因为考虑到需要实现首页界面的左右抽屉控制器切换效果,我们将几个控制器放入一个容器类里面,如下:

@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];self.window.backgroundColor = [UIColor whiteColor];[self.window makeKeyAndVisible];//初始化新浪微博对象self.sinaWeibo = [[SinaWeibo alloc] initWithAppKey:kAppKey appSecret:kAppSecret appRedirectURI:kAppRedirectURI andDelegate:self];//取出本地保存的认证信息NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];NSDictionary *sinaweiboInfo = [defaults objectForKey:@"SinaWeiboAuthData"];if ([sinaweiboInfo objectForKey:@"AccessTokenKey"] && [sinaweiboInfo objectForKey:@"ExpirationDateKey"] && [sinaweiboInfo objectForKey:@"UserIDKey"]){self.sinaWeibo.accessToken = [sinaweiboInfo objectForKey:@"AccessTokenKey"];self.sinaWeibo.expirationDate = [sinaweiboInfo objectForKey:@"ExpirationDateKey"];self.sinaWeibo.userID = [sinaweiboInfo objectForKey:@"UserIDKey"];}//修改容器控制器LeftViewController *leftVC = [[LeftViewController alloc] init];RightViewController *rightVC = [[RightViewController alloc] init];MainViewController *centerVC = [[MainViewController alloc] init];[centerVC.tabBar setClipsToBounds:YES];MMDrawerController *mainVC = [[MMDrawerController alloc] initWithCenterViewController:centerVC leftDrawerViewController:leftVC rightDrawerViewController:rightVC];//1.设置右边控制器的显示宽度[mainVC setMaximumRightDrawerWidth:100];//2.设置左边控制器显示的宽度[mainVC setMaximumLeftDrawerWidth:100];//3.设置打开、关闭的手势区域[mainVC setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];[mainVC setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];// //4.设置动画的类型(效果)[[MMExampleDrawerVisualStateManager sharedManager] setLeftDrawerAnimationType:MMDrawerAnimationTypeParallax];[[MMExampleDrawerVisualStateManager sharedManager] setRightDrawerAnimationType:MMDrawerAnimationTypeParallax];[mainVCsetDrawerVisualStateBlock:^(MMDrawerController *drawerController, MMDrawerSide drawerSide, CGFloat percentVisible) {MMDrawerControllerDrawerVisualStateBlock block;block = [[MMExampleDrawerVisualStateManager sharedManager]drawerVisualStateBlockForDrawerSide:drawerSide];if(block){block(drawerController, drawerSide, percentVisible);}}];self.window.rootViewController = mainVC;return YES;}



③、

UITabBarController :

@interface MainViewController ()<SinaWeiboRequestDelegate>@end@implementation MainViewController {    ThemeImageView *_tabbarView;    ThemeImageView *_selectImgView;    ThemeImageView *_badgeView;}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    [self _createViewControllers];    [self _createTabbarView];        [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];}- (void)_createTabbarView {        for (UIView *view in self.tabBar.subviews) {        Class cls = NSClassFromString(@"UITabBarButton");        if ([view isKindOfClass:cls]) {            [view removeFromSuperview];        }    }        _tabbarView = [[ThemeImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 49)];    _tabbarView.imageName = @"mask_navbar.png";    [self.tabBar setClipsToBounds:YES];    [self.tabBar addSubview:_tabbarView];        _selectImgView = [[ThemeImageView alloc] initWithFrame:CGRectMake(0, 0, 64, 49)];    _selectImgView.imageName = @"home_bottom_tab_arrow.png";    [self.tabBar addSubview:_selectImgView];        NSArray *imgNames = @[                          @"home_tab_icon_1.png",                          @"home_tab_icon_2.png",                          @"home_tab_icon_3.png",                          @"home_tab_icon_4.png",                          @"home_tab_icon_5.png",                          ];        CGFloat itemWidth = kScreenWidth / imgNames.count;    for (int i = 0; i < imgNames.count; i++) {        ThemeButton *button = [[ThemeButton alloc] initWithFrame:CGRectMake(itemWidth*i, 0, itemWidth, 49)];                button.imgName = imgNames[i];        button.tag = i;        [button addTarget:self action:@selector(selectTab:) forControlEvents:UIControlEventTouchUpInside];        [self.tabBar addSubview:button];            }}- (void)selectTab:(UIButton *)button {    [UIView animateWithDuration:0.3 animations:^{        _selectImgView.center = button.center;    }];    self.selectedIndex = button.tag;        CATransition *tren = [[CATransition alloc]init];        tren.type =kCATransitionReveal;//    tren.type = @"suckEffect";//    tren.type = @"rippleEffect";//    tren.type = @"cameraIrisHollowOpen";//    tren.type = @"cameraIrisHollowOpen";//    tren.type = @"cameraIrisHollowOpen";        tren.subtype = kCAGravityResizeAspectFill;    tren.duration = 0.3;    [self.view.layer addAnimation:tren forKey:nil];}- (void)_createViewControllers {    NSArray *storyboardname = @[@"Home",@"Message",@"Profile",@"Disconver",@"More"];    NSMutableArray *viewControllers = [NSMutableArray arrayWithCapacity:5];    for (int i = 0; i < storyboardname.count; i++) {        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardname[i] bundle:nil];        WXNavigationController *navigation = [storyboard instantiateInitialViewController];                [viewControllers addObject:navigation];    }     self.viewControllers = viewControllers;}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}#pragma mark - timerAction- (void)timeAction {    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;    SinaWeibo *sinaweibo = appDelegate.sinaWeibo;    [sinaweibo requestWithURL:unread_count                       params:nil                   httpMethod:@"GET"                     delegate:self];    }- (void)request:(SinaWeiboRequest *)request didFinishLoadingWithResult:(id)result {    if (_badgeView == nil) {        _badgeView = [[ThemeImageView alloc] initWithFrame:CGRectMake(64-32, 0, 32, 32)];        _badgeView.imageName = @"number_notify_9.png";        [self.tabBar addSubview:_badgeView];                                ThemeLabel *badgeLabel = [[ThemeLabel alloc] initWithFrame:_badgeView.bounds];        badgeLabel.colorName = @"Timeline_Notice_color";        badgeLabel.textAlignment = NSTextAlignmentCenter;        badgeLabel.font = [UIFont boldSystemFontOfSize:13.0f];        badgeLabel.backgroundColor = [UIColor clearColor];        badgeLabel.tag = 100;        [_badgeView addSubview:badgeLabel];            }        NSNumber *status = result[@"status"];    NSInteger unRead = [status integerValue];        if (unRead > 0) {        _badgeView.hidden = NO;        ThemeLabel *badaeLabel = (ThemeLabel *)[_badgeView viewWithTag:100];        if (unRead > 100) {            unRead = 99;        }        badaeLabel.text = [status stringValue];            }else {        _badgeView.hidden = YES;        }}- (void)hideBage {    _badgeView.hidden = YES;}@end


④、

UINavigationController

@interface WXNavigationController ()@end@implementation WXNavigationController- (void)dealloc{    [[NSNotificationCenter defaultCenter] removeObserver:self name:kThemeDidChangeNotification object:nil];}- (id)initWithCoder:(NSCoder *)aDecoder {    self = [super initWithCoder:aDecoder];    if (self) {        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadImage) name:kThemeDidChangeNotification object:nil];    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    [self loadImage];    }- (void)loadImage {     NSString *imgName = @"mask_titlebar.png";    if (ios7) {        imgName = @"mask_titlebar64.png";    }    UIImage *img =[[ThemeManager shareInstance] getThemeImage:imgName];    [self.navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];             //3.获取当前主题下的颜色    UIColor *titleColor = [[ThemeManager shareInstance] getThemeColor:@"Mask_Title_color"];    NSDictionary *titleTextAttributes = @{                                          NSForegroundColorAttributeName:titleColor                                                                                    };    self.navigationBar.titleTextAttributes = titleTextAttributes;         //设置返回按钮标题的颜色    if (ios7) {        self.navigationBar.tintColor = [[ThemeManager shareInstance] getThemeColor:@"Mask_Title_color"];    }}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}


项目需要用到的框架 :



做好准备工作后,我们就可以开始 具体内容的具体实现了~


0 0
原创粉丝点击