(Objective-C) 仿閑魚APP,中間凸起tab頁面

1)前言

仿閑魚APP己儒,中間凸起Tab頁面讶凉;

屏幕快照 2019-09-30 下午4.54.48.png

2)解決方案

首先創(chuàng)建CCTabBarController繼承于UITabBarController声滥,
然后和常規(guī)一樣創(chuàng)建5個(gè)Item,中間的按鈕不設(shè)置圖片跺株,代碼如下

  //添加子控制器
- (void)addChildViewControllers{
   CCFishViewController *fish = [[CCFishViewController alloc] init];
   fish.tabBarItem.title = @"閑魚";
   fish.tabBarItem.image = [UIImage imageNamed:@"home_normal"];
   fish.tabBarItem.selectedImage = [UIImage imageNamed:@"home_highlight"];

   CCPondViewController *pond = [[CCPondViewController alloc] init];
   pond.tabBarItem.title = @"魚塘";
   pond.tabBarItem.image = [UIImage imageNamed:@"fishpond_normal"];
   pond.tabBarItem.selectedImage = [UIImage imageNamed:@"fishpond_highlight"];
   //不設(shè)置圖片,先占位
  CCReleaseViewController *release = [[CCReleaseViewController alloc] init];
  release.tabBarItem.title = @"發(fā)布";

  CCNewsViewController *news = [[CCNewsViewController alloc] init];
  news.tabBarItem.title = @"消息";
  news.tabBarItem.image = [UIImage imageNamed:@"message_normal"];
  news.tabBarItem.selectedImage = [UIImage imageNamed:@"message_highlight"];

  CCManagerViewController *manager = [[CCManagerViewController alloc] init];
  manager.tabBarItem.title = @"我的";
  manager.tabBarItem.image = [UIImage imageNamed:@"account_normal"];
  manager.tabBarItem.selectedImage = [UIImage imageNamed:@"account_highlight"];

  NSArray *itemArrays   = @[fish,pond,release,news,manager];
  self.viewControllers  = itemArrays;
  self.tabBar.tintColor = [UIColor blackColor];
}

這樣實(shí)現(xiàn)后苍日,應(yīng)該是這個(gè)樣子的

屏幕快照 2019-09-30 下午4.07.36.png

3)添加凸起按鈕

現(xiàn)在我們可以在UITabBar上添加凸起按鈕,讓他的位置在沒有設(shè)置的中間按鈕偏上宅楞,
按鈕的點(diǎn)擊事件和中間按鈕點(diǎn)擊事件綁定针姿。
??這樣直接添加會有問題,
①因?yàn)樘砑拥陌粹o超過了UITabBar的父視圖厌衙,這樣超過了區(qū)域點(diǎn)擊按鈕會沒有響應(yīng)(如圖紅色區(qū)域所示)


屏幕快照 2019-09-30 下午4.04.41.png

②由于UITabBar是只讀的距淫,所以我們不能直接對他進(jìn)行賦值,利用KVC訪問私有變量將CCTabBar賦值給 UITabBar

代碼實(shí)現(xiàn)如下

CCTabBar.h文件

@interface CCTabBar : UITabBar
@property (nonatomic,strong)UIButton *centerBtn;//中間按鈕
@end

CCTabBar.m文件

- (instancetype)init{
   self = [super init];
   if (self) {
    [self setUI];
   }
   return self;
}

//設(shè)置UI布局
- (void)setUI{
  self.centerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  //設(shè)置button大小為圖片尺寸
  UIImage *normalImage = [UIImage imageNamed:@"post_normal"];
  self.centerBtn.frame = CGRectMake(0,0,normalImage.size.width,normalImage.size.height);
  [self.centerBtn setImage:normalImage forState:UIControlStateNormal];
  //去除選擇時(shí)高亮
  self.centerBtn.adjustsImageWhenHighlighted = NO;
  //設(shè)置圖片位置居中顯示
  self.centerBtn.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - normalImage.size.width)/2, - normalImage.size.height/2,normalImage.size.width, normalImage.size.height);
  [self addSubview:self.centerBtn];
}

//解決超出superView點(diǎn)擊無效問題
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
  UIView *view = [super hitTest:point withEvent:event];
  if (!view){
    //轉(zhuǎn)換坐標(biāo)
    CGPoint tempPoint = [self.centerBtn convertPoint:point fromView:self];
    //判斷點(diǎn)擊的點(diǎn)是否在按鈕區(qū)域內(nèi)
    if (CGRectContainsPoint(self.centerBtn.bounds, tempPoint)){
        //返回按鈕
        return self.centerBtn;
    }
  }
  return view;
}

利用KVC賦值

// CCTabBarController.m文件

 @interface CCTabBarController ()<UITabBarControllerDelegate>
 @property (nonatomic,strong)CCTabBar    *tabBar;
 @end

 - (void)viewDidLoad {
   [super viewDidLoad];

   self.delegate = self;

   self.tabBar = [[CCTabBar alloc] init];
   [self.tabBar.centerBtn addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
   //設(shè)置背景顏色不透明
   self.tabBar.translucent = NO;
   //利用KVC,將自定義tabBar,賦給系統(tǒng)tabBar
   [self setValue:self.tabBar forKeyPath:@"tabBar"];

   [self addChildViewControllers];
}

點(diǎn)擊按鈕事件

- (void)buttonAction{
   //關(guān)聯(lián)中間按鈕
   self.selectedIndex = 2;
   //播放動畫
   [self rotationAnimation];
}

tabBar相關(guān)代理事件

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
   if (tabBarController.selectedIndex == 2){
    //選中中間的按鈕
    [self rotationAnimation];
   }else {
    [self.tabBar.centerBtn.layer removeAllAnimations];
   }
}

旋轉(zhuǎn)動畫

- (void)rotationAnimation{
  CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI*0.5];
  rotationAnimation.duration = 1.0;
  rotationAnimation.repeatCount = DOMAIN;
  [self.tabBar.centerBtn.layer addAnimation:rotationAnimation forKey:@"key"];
}

4)相關(guān)連接

Demo地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末婶希,一起剝皮案震驚了整個(gè)濱河市榕暇,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖彤枢,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件狰晚,死亡現(xiàn)場離奇詭異,居然都是意外死亡缴啡,警方通過查閱死者的電腦和手機(jī)壁晒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來业栅,“玉大人秒咐,你說我怎么就攤上這事〉庠#” “怎么了携取?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長娘汞。 經(jīng)常有香客問我歹茶,道長,這世上最難降的妖魔是什么你弦? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任惊豺,我火速辦了婚禮,結(jié)果婚禮上禽作,老公的妹妹穿的比我還像新娘尸昧。我一直安慰自己,他們只是感情好旷偿,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布烹俗。 她就那樣靜靜地躺著,像睡著了一般萍程。 火紅的嫁衣襯著肌膚如雪幢妄。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天茫负,我揣著相機(jī)與錄音蕉鸳,去河邊找鬼。 笑死忍法,一個(gè)胖子當(dāng)著我的面吹牛潮尝,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播饿序,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼勉失,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了原探?” 一聲冷哼從身側(cè)響起乱凿,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤顽素,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后徒蟆,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體戈抄,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年后专,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了划鸽。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,779評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡戚哎,死狀恐怖裸诽,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情型凳,我是刑警寧澤丈冬,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站甘畅,受9級特大地震影響埂蕊,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜疏唾,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一蓄氧、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧槐脏,春花似錦喉童、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至牌废,卻和暖如春咽白,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背鸟缕。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工晶框, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人叁扫。 一個(gè)月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓三妈,卻偏偏與公主長得像畜埋,于是被迫代替她去往敵國和親莫绣。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內(nèi)容