先上效果圖
思路是個很重要的東西
--->第一、如何實現中間按鈕的與眾不同,怎樣改變?
--->第二、實現完界面的處理之后,事件怎么處理相應?
??????? 首先來解決第一個問題,通過視圖層級關系我們可以tabBar上面一個一個的item是UITabBarButton,而UITabBarButton子視圖是UITabBarSwappableImageView和UITabBarButtonLabel,分別用來顯示圖片和文字。label我們是不需要去處理的,主要就是改變imageView的frame。通過各種姿勢發現修改不了這個frame,所以我們用一種投機取巧的方式來解決之,自定義一個ImageView放在UITabBarButton上(然后直接把UITabBarSwappableImageView)隱藏掉即可。代碼如下:
-? (instancetype)initWithImage:(UIImage *)image{
if (self = [super init]) {
_imageView = [[UIImageView alloc] initWithImage:image];
}
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
NSInteger btnIndex = 0;
Class class = NSClassFromString(@"UITabBarButton");
for (UIView *btn in self.subviews) {
if ([btn isKindOfClass:class]) {
if (btnIndex == 2) {
UIImageView *view = btn.subviews.firstObject;
view.center = CGPointMake(btn.size.width/2.0, 0);
_imageView.frame = view.frame;
[btn addSubview:_imageView];
view.hidden = YES;
_imageView.hidden = NO;
}
btnIndex ++;
}
}
}
??????? 經過上面的處理,界面達到了我們想要的效果,不完美的是中間按鈕突出部分點擊沒反應,各個tabbar切換自如,但是中間的圖片不能自主切換。首先解決第一個問題,相應超出父視圖部分,代碼如下:
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if (self.isHidden == NO)
{
CGPoint newA = [self convertPoint:point toView:self.imageView];
if ( [self.imageView pointInside:newA withEvent:event])
{
return self.imageView.superview;
}
else
{
return [super hitTest:point withEvent:event];
}
}
else
{
return [super hitTest:point withEvent:event];
}
}
然后再解決點擊中間bar選中圖片切換的問題,只需要實現tabbarViewController代理方法即可,代碼如下:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
if ([self.viewControllers[2] isEqual:viewController]) {
_TABBAR.imageView.image = [UIImage imageNamed:@"按壓-邂逅"];
}else{
_TABBAR.imageView.image = [UIImage imageNamed:@"邂逅"];
}
}
由于barItem還是原來的barItem,故不需要去處理多余的事件,完美解決