开发常用集锦(不定期更新)

1. 绘制阴影色

在平时开发中,我们经常拿到这样的设计图 , 重点在于经常忽略的阴影色

屏幕快照 2017-08-31 14.46.30.png
代码或者sb 中,此控件的masksToBounds = NO

实现代码

    CALayer * layer = [self.goContinueBtn layer];
    [layer setShadowOffset:CGSizeMake(0, 5)]; //为阴影偏移量,默认为(左右,上下)
    [layer setShadowRadius:4];// 为阴影四角圆角半径,默认值为
    [layer setShadowOpacity:0.5]; //为阴影透明度(取值为[0,1])
    [layer setShadowColor:[[admore_tools getInstance] colorWithHexString:@"248EFF"].CGColor]; //为阴影颜色

2.关于色值问题

  • 使用sb 或者xib 中吸色工具
    在实际开发中发现,xcode 的吸色是比较方便,但是有时候会色差,吸出的颜色与设计稿不一致
  • 使用万能代码设置

开发中,不要担心设计给你的颜色值是 RGB 还是 16进制 的 ,我们都可以使用的,一样方便.
下方即是使用方法

#define COLOR(inColor)          RGBA((unsigned char) (inColor >> 16), (unsigned char) (inColor >> 8), (unsigned char) (inColor), 1.0)
#define RGBA(r,g,b,a)           [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
#define RGB(r,g,b)              RGBA(r,g,b,1.0)

隐藏导航条的 阴影线

 [self.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
 [self.navigationBar setShadowImage:[[UIImage alloc] init]];

获取当前APP的图标信息

 NSDictionary *infoPlist = [[NSBundle mainBundle] infoDictionary];
 NSString *icon = [[infoPlist valueForKeyPath:@"CFBundleIcons.CFBundlePrimaryIcon.CFBundleIconFiles"] lastObject];
 self.imageView.image = [UIImage imageNamed:icon];

mas 获取导航栏的高度

    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    CGRect rectNav = self.navigationController.navigationBar.frame;

关于设置圆角问题(设置特定位置圆角)

 UIRectCorner corner = UIRectCornerTopLeft | UIRectCornerTopRight;
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.view.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(12, 12)];
CAShapeLayer * shapeLayer = [CAShapeLayer layer];
shapeLayer.path = path.CGPath;
self.view.layer.mask = shapeLayer;

获取当前页面的ViewController

- (UIViewController *)currentViewController{

    UIViewController* vc = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (1) {
        if ([vc isKindOfClass:[UITabBarController class]]) {
            vc = ((UITabBarController*)vc).selectedViewController;
        }
        if ([vc isKindOfClass:[UINavigationController class]]) {
            vc = ((UINavigationController*)vc).visibleViewController;
        }
        if (vc.presentedViewController) {
            vc = vc.presentedViewController;
        }else{
            break;
        }
    }
    return vc;
}

使用颜色生成背景图片(设置button等)

- (UIImage *)getGradientImageFromColors:(NSArray*)colors  imgSize:(CGSize)imgSize
{
    CGRect rect = (CGRect){0.f, 0.f, imgSize};
    NSMutableArray *ar = [NSMutableArray array];
    for(UIColor *c in colors) {
        [ar addObject:(id)c.CGColor];
    }
    UIGraphicsBeginImageContextWithOptions(imgSize, NO, UIScreen.mainScreen.scale);
    CGContextAddPath(UIGraphicsGetCurrentContext(),
                     [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:24].CGPath);
    CGContextClip(UIGraphicsGetCurrentContext());
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL);

    CGPoint   start = CGPointMake(0.0, 0.0);
    CGPoint  end = CGPointMake(imgSize.width, 0.0);

    CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    CGGradientRelease(gradient);
    CGContextRestoreGState(context);
    CGColorSpaceRelease(colorSpace);
    UIGraphicsEndImageContext();
    return image;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容