类似闲鱼、QQ空间中间凸起的TabBar
2018-09-18 本文已影响662人
喝酸奶舔下盖
虽然现在主流app很少做这种设计,但是我确实碰到了,还不止一次,上次随便写写没怎么总结,这次稍微整理了下。和闲鱼、qq空间不同的是需求要求中间item对应了一个VC,而闲鱼、QQ空间是中间item对应一个按钮。不足之处大佬们多多批评指正。
样式
gif5新文件.gif思路
上移中间tabBarItem的范围,同时扩大它的点击范围,只有中间的才会扩大,item为偶数的话是正常显示的。实现代码
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
if (!self.isUserInteractionEnabled || self.isHidden || self.alpha <= 0.01)
{
return nil;
}
if (self.clipsToBounds && ![self pointInside:point withEvent:event]) {
return nil;
}
if ([self pointInside:point withEvent:event])
{
return [self mmHitTest:point withEvent:event];
}
else
{
CGFloat tabBarItemWidth = self.bounds.size.width/self.items.count;
CGFloat left = self.center.x - tabBarItemWidth/2;
CGFloat right = self.center.x + tabBarItemWidth/2;
if (point.x < right &&
point.x > left)
{//当点击的point的x坐标是中间item范围内,才去修正落点
CGPoint otherPoint = CGPointMake(point.x, point.y + self.effectAreaY);
return [self mmHitTest:otherPoint withEvent:event];
}
}
return nil;
}
- (UIView *)mmHitTest:(CGPoint)point withEvent:(UIEvent *)event
{
for (UIView *subview in [self.subviews reverseObjectEnumerator])
{
CGPoint convertedPoint = [subview convertPoint:point fromView:self];
UIView *hitTestView = [subview hitTest:convertedPoint withEvent:event];
if (hitTestView) {
return hitTestView;
}
}
return nil;
}
更改TabBar样式,选中某个TabBarItem添加动画
- (void)uiSetting {
// 更换tabBar
CustomTabBar *myTabBar = [[CustomTabBar alloc] init];
myTabBar.effectAreaY = 35;
[self setValue:myTabBar forKey:@"tabBar"];
NSMutableArray *mArr = [[NSMutableArray alloc] init];
for (NSInteger i = 0;i < self.tabBarViewControllers.count; i++) {
NSString *imageNormal = [NSString stringWithFormat:@"%@",_tabBarItemsAttributes[i][WXWTabBarItemImage]];
NSString *imageSelected = [NSString stringWithFormat:@"%@",_tabBarItemsAttributes[i][WXWTabBarItemSelectedImage]];
UIViewController *vc = self.tabBarViewControllers[i];
[vc setTitle:_tabBarItemsAttributes[i][WXWTabBarItemTitle]];
vc.tabBarItem.image = [[UIImage imageNamed:imageNormal] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
vc.tabBarItem.selectedImage = [[UIImage imageNamed:imageSelected] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
NSValue *insetsValue = _tabBarItemsAttributes[i][WXWTabBarItemImageInsets];
UIEdgeInsets insets = [insetsValue UIEdgeInsetsValue];
[vc.tabBarItem setImageInsets:insets];//修改图片偏移量,上下,左右必须为相反数,否则图片会被压缩
NSValue *offsetValue = _tabBarItemsAttributes[i][WXWTabBarItemTitlePositionAdjustment];
UIOffset offset = [offsetValue UIOffsetValue];
[vc.tabBarItem setTitlePositionAdjustment:offset];//修改文字偏移量
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
nav.title = _tabBarItemsAttributes[i][WXWTabBarItemTitle];
[mArr addObject:nav];
}
self.viewControllers = mArr;
self.selectedIndex = 0;
self.mItemArray = nil;
for (UIView *btn in self.tabBar.subviews) {
if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
[self.mItemArray addObject:btn];
}
}
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSInteger index = [self.tabBar.items indexOfObject:item];
if (index != self.indexFlag) {
UIButton *btn = self.mItemArray[index];
UIView *animationView = [btn wxw_tabImageView];
if (index == 1 && self.selectedAnimation) {
[self addScaleAnimationOnView:animationView repeatCount:1];
} else if (index != 1 && self.selectedAnimation) {
[self addRotateAnimationOnView:animationView];
}
[self.tabBar bringSubviewToFront:self.mItemArray[self.indexFlag]];
self.indexFlag = index;
}
}
//缩放动画
- (void)addScaleAnimationOnView:(UIView *)animationView repeatCount:(float)repeatCount {
//需要实现的帧动画,这里根据需求自定义
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"transform.scale";
animation.values = @[@1.0,@1.3,@0.9,@1.15,@0.95,@1.02,@1.0];
animation.duration = 1;
animation.repeatCount = repeatCount;
animation.calculationMode = kCAAnimationCubic;
[animationView.layer addAnimation:animation forKey:nil];
}
//旋转Y动画
- (void)addRotateAnimationOnView:(UIView *)animationView {
// 针对旋转动画,需要将旋转轴向屏幕外侧平移,最大图片宽度的一半
// 否则背景与按钮图片处于同一层次,当按钮图片旋转时,转轴就在背景图上,动画时会有一部分在背景图之下。
// 动画结束后复位
animationView.layer.zPosition = 65.f / 2;
[UIView animateWithDuration:0.32 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
animationView.layer.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);
} completion:nil];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.70 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0.2 options:UIViewAnimationOptionCurveEaseOut animations:^{
animationView.layer.transform = CATransform3DMakeRotation(2 * M_PI, 0, 1, 0);
} completion:nil];
});
}
使用
新建一个对象,用于更改Tabbar的样式配置,在delegate的didFinishLaunchingWithOptions方法中把自定义的TabBarController设置为根视图
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
WXWTabBarControllerConfig *tabBarControllerConfig = [[WXWTabBarControllerConfig alloc] init];
CustomTabBarController *tabBarController = tabBarControllerConfig.tabBarController;
self.window.rootViewController = tabBarController;
return YES;
}
样式可以根据自己的需求调整,具体实现如下
#import <Foundation/Foundation.h>
#import "CustomTabBarController.h"
@interface WXWTabBarControllerConfig : NSObject
@property (nonatomic, readonly, strong) CustomTabBarController *tabBarController;
@end
#import "WXWTabBarControllerConfig.h"
//十六进制颜色
#define ColorFromHexValue(hexValue) [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0 green:((float)((hexValue & 0xFF00) >> 8))/255.0 blue:((float)(hexValue & 0xFF))/255.0 alpha:1.0]
@interface WXWTabBarControllerConfig()
@property (strong, readwrite, nonatomic) CustomTabBarController *tabBarController;
@end
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
@implementation WXWTabBarControllerConfig
/**
* lazy load tabBarController
*
* @return WXWTabBarController
*/
- (CustomTabBarController *)tabBarController {
if (_tabBarController == nil) {
/**
* 以下两行代码目的在于手动设置让TabBarItem只显示图标,不显示文字,并让图标垂直居中。
* 等效于在 `-tabBarItemsAttributesForController` 方法中不传 `WXWTabBarItemTitle` 字段。
* 更推荐后一种做法。
*/
UIEdgeInsets imageInsets = UIEdgeInsetsZero;//UIEdgeInsetsMake(4.5, 0, -4.5, 0);
UIOffset titlePositionAdjustment = UIOffsetZero;//UIOffsetMake(0, MAXFLOAT);
CustomTabBarController *tabBarController = [CustomTabBarController tabBarControllerWithViewControllers:self.viewControllers
tabBarItemsAttributes:self.tabBarItemsAttributesForController
imageInsets:imageInsets
titlePositionAdjustment:titlePositionAdjustment
];
[self customizeTabBarAppearance:tabBarController];
tabBarController.selectedAnimation = YES; //选中动画关闭
_tabBarController = tabBarController;
}
return _tabBarController;
}
- (NSArray *)viewControllers {
FirstViewController *firstViewController = [[FirstViewController alloc] init];
SecondViewController *secondViewController = [[SecondViewController alloc] init];
ThirdViewController *thirdViewController = [[ThirdViewController alloc] init];
NSArray *viewControllers = @[
firstViewController,
secondViewController,
thirdViewController
];
return viewControllers;
}
- (NSArray *)tabBarItemsAttributesForController {
NSDictionary *firstTabBarItemsAttributes = @{
WXWTabBarItemTitle : @"加速",
WXWTabBarItemImage : @"加速未选中", /* NSString and UIImage are supported*/
WXWTabBarItemSelectedImage : @"加速选中", /* NSString and UIImage are supported*/
WXWTabBarItemImageInsets: [NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)],
WXWTabBarItemTitlePositionAdjustment: [NSValue valueWithUIOffset:UIOffsetMake(0, 0)]
};
NSDictionary *secondTabBarItemsAttributes = @{
WXWTabBarItemTitle : @"",
WXWTabBarItemImage : @"游戏列表未选中",
WXWTabBarItemSelectedImage : @"游戏列表选中",
WXWTabBarItemImageInsets: [NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)],
WXWTabBarItemTitlePositionAdjustment: [NSValue valueWithUIOffset:UIOffsetMake(0, 0)]
};
NSDictionary *thirdTabBarItemsAttributes = @{
WXWTabBarItemTitle : @"我的",
WXWTabBarItemImage : @"我的未选中",
WXWTabBarItemSelectedImage : @"我的选中",
WXWTabBarItemImageInsets: [NSValue valueWithUIEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)],
WXWTabBarItemTitlePositionAdjustment: [NSValue valueWithUIOffset:UIOffsetMake(0, 0)]
};
NSArray *tabBarItemsAttributes = @[
firstTabBarItemsAttributes,
secondTabBarItemsAttributes,
thirdTabBarItemsAttributes,
];
return tabBarItemsAttributes;
}
/**
* 更多TabBar自定义设置:比如:tabBarItem 的选中和不选中文字和背景图片属性、tabbar 背景图片属性等等
*/
- (void)customizeTabBarAppearance:(CustomTabBarController *)tabBarController {
// set the text color for unselected state
// 普通状态下的文字属性
NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
normalAttrs[NSForegroundColorAttributeName] = ColorFromHexValue(0x8e8e8e);
// set the text color for selected state
// 选中状态下的文字属性
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
// set the text Attributes
// 设置文字属性
UITabBarItem *tabBar = [UITabBarItem appearance];
[tabBar setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
[tabBar setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
[[UITabBar appearance] setBackgroundColor:ColorFromHexValue(0x262626)];
[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
}
+ (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize {
CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width * scaleSize, image.size.height * scaleSize);
UIGraphicsBeginImageContextWithOptions(size, NO, 1.0);
[image drawInRect:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width * scaleSize, image.size.height * scaleSize)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
if (!color || size.width <= 0 || size.height <= 0) return nil;
CGRect rect = CGRectMake(0.0f, 0.0f, size.width + 1, size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
WXWTabBarController.gif
另外,类似闲鱼、QQ空间中间按钮不和VC关联也写了个小demo: