美文网首页
【iOS】Moots记录

【iOS】Moots记录

作者: Colleny_Z | 来源:发表于2018-10-29 10:50 被阅读11次
1. 隐藏系统的tabbar黑线

其中若有设自定义的背景图片(self.tabbarBgImg),请注意勿移错。
此方法如果是自定义tabbar请在自定义tabbar的layoutSubviews中调用,若是没有请在自定义tabbarController中的viewDidAppear中调用。

- (void)seekTopLineHidden{
    for (UIView *bav  in self.subviews) {
        if ([bav isKindOfClass:NSClassFromString(@"_UIBarBackground")]) {
            for (UIView *imgV in bav.subviews) {
                if ([imgV isKindOfClass:[UIImageView class]]) {
                    UIImageView * img = (UIImageView *)imgV;
                    if (img.image != self.tabbarBgImg) {
                        imgV.hidden = YES;
                        [self.topLineView removeFromSuperview];
                        break;
                    }
                }
            }
        }
    }
2. iPhoneX判断
static inline BOOL isIPhoneX() {
                BOOL iPhoneX = NO; 
                /// 先判断设备是否是iPhone/iPod 
                if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) { 
                       return iPhoneX; 
                }
              if (@available(iOS 11.0, *)) { 
                /// 利用safeAreaInsets.bottom > 0.0来判断是否是iPhone X。 
                UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window]; 
                  if (mainWindow.safeAreaInsets.bottom > 0.0) { 
                          iPhoneX = YES; 
                  } 
              } 
        return iPhoneX; 
}
3. UIMenuController规则

默认情况下,有以下控件已经支持UIMenuController
UITextFieldUITextViewUIWebView

如果让其他控件也支持UIMenuController(比如UILabel),二步
1.自定义UILabel

/**
 * 让label可以成为第一响应者
 */
- (BOOL)canBecomeFirstResponder
{
    return YES;
}

/**
 * label能只想哪个操作
 */
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    NSLog(@"%@", NSStringFromSelector(action));
    
    if (action == @selector(cut:) || action == @selector(copy:) || action == @selector(paste:)) {
        return YES;
    }
    
    return NO;
}

2.实现操作方法

- (void)copy:(UIMenuController *)menu
{
    /** 将自己的文字赋值到粘贴板 */
    if (self.text.length > 0) {
        UIPasteboard *board = [UIPasteboard generalPasteboard];
        board.string = self.text;
    }
}

- (void)cut:(UIMenuController *)menu
{
    /** 将自己的文字赋值到粘贴板 */
    [self copy:menu];
    
    /** 清空文字 */
    self.text = nil;
    
}

- (void)paste:(UIMenuController *)menu
{
    /** 将粘贴板文字赋值到自己身上 */
    UIPasteboard *board = [UIPasteboard generalPasteboard];
    self.text = board.string;
}

显示MenuController
参考链接:https://www.jianshu.com/p/9ded1b4a5cda

- (void)labelClick
{
    /** Label 要成为第一响应者 (告诉我们menu支持什么操作, 如何处理)*/
    [self becomeFirstResponder];
    
    /** 显示MenuController */
    UIMenuController *menu = [UIMenuController sharedMenuController];
    /** targetRect: 指向的矩形框 */
    [menu setTargetRect:self.frame inView:self.superview];
    [menu setMenuVisible:YES animated:YES];
}
4. 新人引导缕空:
运行效果
     // 测试全屏背景图
    UIImageView * bgImg = [[UIImageView alloc]initWithFrame:self.view.bounds];
    bgImg.image = [UIImage imageNamed:@"111.jpg"];
    [self.view addSubview:bgImg];

    UIView* aView = [[UIView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:aView];

    CAShapeLayer* cropLayer = [[CAShapeLayer alloc] init];
    // 填充规则(重点)
    [cropLayer setFillRule:kCAFillRuleEvenOdd];
    // 填充颜色
    [cropLayer setFillColor:[[UIColor colorWithRed:0 green:0 blue:0 alpha:0.35] CGColor]];
    [aView.layer addSublayer:cropLayer];

    // 缕空矩形
    CGRect cropRect = CGRectMake(100, 100, 160, 140);

    CGMutablePathRef path =CGPathCreateMutable();
    CGPathAddRect(path, nil, aView.bounds);
    CGPathAddRect(path, nil, cropRect);

    [cropLayer setPath:path];
5. Pod资源文件的正确Bundle读取
@implementation NSBundle (AssociatedBundle)
/**
 获取文件所在name,默认情况下podName和bundlename相同,传一个即可
 
 @param bundleName bundle名字,就是在resource_bundles里面的名字
 @param podName pod的名字
 @return bundle
 */
+ (NSBundle *)bundleWithBundleName:(NSString *)bundleName podName:(NSString *)podName{
    if (bundleName == nil && podName == nil) {
        @throw @"bundleName和podName不能同时为空";
    }else if (bundleName == nil ) {
        bundleName = podName;
    }else if (podName == nil) {
        podName = bundleName;
    }
    
    
    if ([bundleName containsString:@".bundle"]) {
        bundleName = [bundleName componentsSeparatedByString:@".bundle"].firstObject;
    }
    //没使用framwork的情况下
    NSURL *associateBundleURL = [[NSBundle mainBundle] URLForResource:bundleName withExtension:@"bundle"];
    //使用framework形式
    if (!associateBundleURL) {
        associateBundleURL = [[NSBundle mainBundle] URLForResource:@"Frameworks" withExtension:nil];
        associateBundleURL = [associateBundleURL URLByAppendingPathComponent:podName];
        associateBundleURL = [associateBundleURL URLByAppendingPathExtension:@"framework"];
        NSBundle *associateBunle = [NSBundle bundleWithURL:associateBundleURL];
        associateBundleURL = [associateBunle URLForResource:bundleName withExtension:@"bundle"];
    }
    
    NSAssert(associateBundleURL, @"取不到关联bundle");
    //生产环境直接返回空
    return associateBundleURL?[NSBundle bundleWithURL:associateBundleURL]:nil;
}
@end

6. 视图圆角
@implementation UIView (RounderCorner)

- (void)dlj_addRounderCornerWithRadius:(CGFloat)radius size:(CGSize)size
{
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextRef cxt = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(cxt, [UIColor redColor].CGColor);
    CGContextSetStrokeColorWithColor(cxt, [UIColor redColor].CGColor);
    
    CGContextMoveToPoint(cxt, size.width, size.height-radius);
    CGContextAddArcToPoint(cxt, size.width, size.height, size.width-radius, size.height, radius);//右下角
    CGContextAddArcToPoint(cxt, 0, size.height, 0, size.height-radius, radius);//左下角
    CGContextAddArcToPoint(cxt, 0, 0, radius, 0, radius);//左上角
    CGContextAddArcToPoint(cxt, size.width, 0, size.width, radius, radius);//右上角
    CGContextClosePath(cxt);
    CGContextDrawPath(cxt, kCGPathFillStroke);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    [imageView setImage:image];
    [self insertSubview:imageView atIndex:0];
}
@implementation UIImage (ImageRoundedCorner)

- (UIImage*)imageAddCornerWithRadius:(CGFloat)radius andSize:(CGSize)size{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(radius, radius)];
    CGContextAddPath(ctx,path.CGPath);
    CGContextClip(ctx);
    [self drawInRect:rect];
    CGContextDrawPath(ctx, kCGPathFillStroke);
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

7. 证书冲突
  • 错误提示

XXX is automatically signed, but provisioning profile XXX has been manually specified. Set the provisioning profile value to “Automatic” in the build settings editor, or switch to manual signing in the project editor.
其中XXX是项目名称,XXX是一串字符(provisioning描述文件的名称)。

  • 解决方法
  1. 删TARGETS->Building Setting->Code Signing->CODE_SIGN_ENTITLEMENTS 所示的 Entitlements直接删除
  2. 打开"项目名.xcodeproj",然后右键,显示包内容,打开:project.pbxproj 搜索上述一连串数字内容删除即可,只删除这一串字符串就行,不要删除引号及引号外的字符。
  3. 最后重新编绎
7. PNG图片渲染圆角边框那些事儿
- (UIImage *)drawImg:(UIImage *)image size:(CGSize)size backgroundColor:(UIColor *)color{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);

    // 开启上下文,这里若设置NO也可以,但是会影响性能,所以在clip前用背景色填充
    UIGraphicsBeginImageContextWithOptions(size, true, 0);

    // 配置上下文属性
    [color setFill];
    UIRectFill(rect);
    [[UIColor blackColor] setStroke];

    // 指定path,然后绘制
    UIBezierPath * bezierPath = [UIBezierPath bezierPathWithOvalInRect:rect];
    bezierPath.lineWidth = 5.0f;
    [bezierPath addClip];

    [image drawInRect:rect];


    // 描边
    [bezierPath stroke];

    UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();

    return resultImg;
}
8. 监听系统导航侧滑返回进度

核心代码:

- (void)viewDidLoad{
    [self.navigationController.interactivePopGestureRecognizer addTarget:self action:@selector(test:)];
}
- (void)test:(UIScreenEdgePanGestureRecognizer *)panGes{
    switch (panGes.state) {
        case UIGestureRecognizerStateChanged:{
            CGFloat x = [panGes translationInView:self.view].x;
            CGFloat progress = x / self.view.frame.size.width;
        }
            break;
        default:
            break;
    }
}

相关文章

网友评论

      本文标题:【iOS】Moots记录

      本文链接:https://www.haomeiwen.com/subject/fntbtqtx.html