仿新浪微博IOS用戶端(v5.2.8)——下拉式功能表欄的實現,iosv5.2.8
轉載請標明出處:http://blog.csdn.net/android_ls/article/details/45877983
聲明:仿新浪微博項目,所用所有圖片資源都來源於官方新浪微博IOS用戶端,編寫本應用的目的在於學習交流,如涉及侵權請告知,我會及時換掉用到的相關圖片。
接著上一篇博文,這篇我們來聊聊新浪微博導覽列上,點擊中間部分的標題(titleView)彈出的下拉式功能表是如何?。
1、自訂導覽列中間的titleView,代碼如下:
// 設定導覽列中間的titleView _titleButton = [self titleViewWithNickname:@"指間有夢"]; self.navigationItem.titleView = _titleButton;
2、在UIButton中預設有imageView和titleLabel兩個子View,預設imageView是在左邊的,而titleLabel是在右邊的。我們看到在新浪微博首頁上,titleView上顯示的是titleLabel在左邊,imageView在titleView的右邊,我們試著調整UIButton中子View的顯示位置,具體實現代碼如下:
#pragma mark 設定導覽列中間的titleView-(UIButton *) titleViewWithNickname:(NSString *)nickname{ UIButton *titleButton = [[UIButton alloc] init]; // 設定圖片和文字 [titleButton setTitle:nickname forState:UIControlStateNormal]; [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; titleButton.titleLabel.font = [UIFont systemFontOfSize:18]; [titleButton setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal]; [titleButton setImage:[UIImage imageNamed:@"navigationbar_arrow_up"] forState:UIControlStateSelected]; // 90 40這兩個值目前是隨便寫的 titleButton.imageEdgeInsets = UIEdgeInsetsMake(0, 90, 0, 0); titleButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 40); // 130這個值目前是隨便寫的,後面要改為根據內容自動計算長度 titleButton.size = CGSizeMake(130, 40);// titleButton.backgroundColor = [UIColor redColor]; [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside]; return titleButton;}
註:不必拘於這種方式,其他方式也是可以實現(比如自訂一個View,往其中添加兩個子View)。
3、調整後的如下:
4、點擊自訂的titleView,彈出下拉式功能表,具體實現代碼如下:
#pragma mark 點擊導覽列上的標題事件處理器- (void)titleClick:(UIButton *)titleButton{ // 1.建立下拉式功能表 DropdownMenuView *dropdownMenuView = [[DropdownMenuView alloc] init]; // 設定下拉式功能表彈出、銷毀事件的監聽者 dropdownMenuView.delegate = self; // 2.設定要顯示的內容 TitleMenuViewController *titleMenuVC = [[TitleMenuViewController alloc] init]; titleMenuVC.dropdownMenuView = dropdownMenuView; titleMenuVC.delegate = self; titleMenuVC.view.width = kMobilePhoneScreenWidth/2; titleMenuVC.view.height = kMobilePhoneScreenHeight/2; dropdownMenuView.contentController = titleMenuVC; // 3.顯示下拉式功能表 [dropdownMenuView showFrom:titleButton];}
5、讓當前控制器實現DropdownMenuDelegate和TitleMenuDelegate協議,實現相應的函數,具體代碼如下:
@interface HomeViewController ()<DropdownMenuDelegate, TitleMenuDelegate>{ UIButton *_titleButton;}@end
#pragma mark - DropdownMenuDelegate#pragma mark 下拉式功能表被銷毀了- (void)dropdownMenuDidDismiss:(DropdownMenuView *)menu{ // 讓指示箭頭向下 UIButton *titleButton = (UIButton *)self.navigationItem.titleView; titleButton.selected = NO;}#pragma mark 下拉式功能表顯示了- (void)dropdownMenuDidShow:(DropdownMenuView *)menu{ // 讓指示箭頭向上 UIButton *titleButton = (UIButton *)self.navigationItem.titleView; titleButton.selected = YES;}#pragma mark - TitleMenuDelegate-(void)selectAtIndexPath:(NSIndexPath *)indexPath title:(NSString *)title{ MyLog(@"indexPath = %ld", indexPath.row); MyLog(@"當前選擇了%@", title); // 修改導覽列的標題 [_titleButton setTitle:title forState:UIControlStateNormal]; // 調用根據搜尋條件返回相應的微博資料 // ...}
6、先來看看已實現的,有圖有真相。
點擊螢幕中除下拉式功能表外的任意地方,或者點擊下拉式功能表中的某一項(比如點擊了“好友圈”),讓下拉式功能表銷毀。
再次點擊titleView,如:
7、下拉式功能表實現核心類(DropdownMenuView.h)的源碼如下:
//// DropdownMenuView.h// SinaWeibo//// Created by android_ls on 15/5/20.// Copyright (c) 2015年 android_ls. All rights reserved.//// 下拉式功能表組件#import <UIKit/UIKit.h>@class DropdownMenuView;@protocol DropdownMenuDelegate <NSObject>@optional- (void)dropdownMenuDidDismiss:(DropdownMenuView *)menu;- (void)dropdownMenuDidShow:(DropdownMenuView *)menu;@end@interface DropdownMenuView : UIView@property (nonatomic, weak) id<DropdownMenuDelegate> delegate;#pragma mark 在指定UIView下方顯示菜單- (void)showFrom:(UIView *)from;#pragma mark 銷毀下拉式功能表- (void)dismiss;// 要顯示的內容控制器@property (nonatomic, strong) UIViewController *contentController;@end
下拉式功能表實現核心類(DropdownMenuView.m)的源碼如下:
//// DropdownMenuView.m// SinaWeibo//// Created by android_ls on 15/5/20.// Copyright (c) 2015年 android_ls. All rights reserved.//#import "DropdownMenuView.h"@interface DropdownMenuView(){ // 用來顯示具體內容的容器 UIImageView * _containerView;}@end@implementation DropdownMenuView- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // 清除預設的背景顏色 self.backgroundColor = [UIColor clearColor]; // 添加一個灰色圖片,作為下拉式功能表的背景 _containerView = [[UIImageView alloc] init]; _containerView.image = [UIImage imageNamed:@"popover_background"]; _containerView.userInteractionEnabled = YES; [self addSubview:_containerView]; } return self;}- (void)setContentController:(UIViewController *)contentController{ _contentController = contentController; UIView * content = contentController.view; // 調整內容的位置 content.x = 7; content.y = 13; _containerView.height = CGRectGetMaxY(content.frame) + 9; _containerView.width = CGRectGetMaxX(content.frame) + 7; // 新增內容到灰色圖片中 [_containerView addSubview:content];}#pragma mark 在指定UIView下方顯示菜單- (void)showFrom:(UIView *)from{ // 1.獲得最上面的視窗 UIWindow *window = [[UIApplication sharedApplication].windows lastObject]; // 2.添加自己到視窗上 [window addSubview:self]; // 3.設定尺寸 self.frame = window.bounds; // 4.調整灰色圖片的位置 // 預設情況下,frame是以父控制項左上方為座標原點 // 轉換座標系 CGRect newFrame = [from convertRect:from.bounds toView:window]; _containerView.centerX = CGRectGetMidX(newFrame); _containerView.y = CGRectGetMaxY(newFrame); // 通知外界,自己顯示了 if ([self.delegate respondsToSelector:@selector(dropdownMenuDidShow:)]) { [self.delegate dropdownMenuDidShow:self]; }}#pragma mark 銷毀下拉式功能表- (void)dismiss{ [self removeFromSuperview]; // 通知外界,自己被銷毀了 if ([self.delegate respondsToSelector:@selector(dropdownMenuDidDismiss:)]) { [self.delegate dropdownMenuDidDismiss:self]; }}#pragma mark 點擊自己執行銷毀動作- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self dismiss];}@end
8、TitleMenuViewController.h的源碼如下:
//// TitleMenuViewController.h// SinaWeibo//// Created by android_ls on 15/5/20.// Copyright (c) 2015年 android_ls. All rights reserved.//#import <UIKit/UIKit.h>@class DropdownMenuView;@protocol TitleMenuDelegate <NSObject>#pragma mark 當前選中了哪一行@required- (void)selectAtIndexPath:(NSIndexPath *)indexPath title:(NSString*)title;@end@interface TitleMenuViewController : UITableViewController@property (nonatomic, weak) id<TitleMenuDelegate> delegate;@property (nonatomic, weak) DropdownMenuView * dropdownMenuView;@end
TitleMenuViewController.m的源碼如下:
//// TitleMenuViewController.m// SinaWeibo//// Created by android_ls on 15/5/20.// Copyright (c) 2015年 android_ls. All rights reserved.//#import "TitleMenuViewController.h"#import "DropdownMenuView.h"@interface TitleMenuViewController ()@property (nonatomic, strong) NSMutableArray * data;@end@implementation TitleMenuViewController- (void)viewDidLoad { [super viewDidLoad]; _data = [NSMutableArray array]; [_data addObject:@"首頁"]; [_data addObject:@"好友圈"]; [_data addObject:@"群微博"]; [_data addObject:@"我的微博"]; [_data addObject:@"特別關注"]; [_data addObject:@"名人明星"]; [_data addObject:@"同事"]; [_data addObject:@"同學"]; [self.tableView reloadData];}#pragma mark - Table view data source- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _data.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"statusCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } NSString * name = _data[indexPath.row]; cell.textLabel.text = name; return cell;}#pragma mark Cell點擊事件處理器-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:YES]; if (_dropdownMenuView) { [_dropdownMenuView dismiss]; } if (_delegate) { [_delegate selectAtIndexPath:indexPath title:_data[indexPath.row]]; } }@end
今晚就到這裡吧,有些累了,我先去睡了,晚安。