美文网首页
iOS-防止离屏渲染

iOS-防止离屏渲染

作者: Arthur澪 | 来源:发表于2020-03-28 10:12 被阅读0次

    UIBezierPath 贝塞尔曲线给 view 绘圆角

    1、绘圆形

    -(void)pd_setRadius{
        [self.superview layoutIfNeeded];   // 确保获取到 bounds
    
         UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:self.bounds.size];
         CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
         maskLayer.frame = self.bounds;
         maskLayer.path = maskPath.CGPath;
         self.layer.mask = maskLayer;
    }
    

    2、绘圆角

    -(void)pd_setRadius:(float)radius{
        
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(radius, radius)];
        
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = bounds;
        maskLayer.path = maskPath.CGPath;
        
        [self.layer setMask: maskLayer];
    }
    

    3、自定义绘圆角

    // 给view的上方两个角 设圆角
    -(void)setTopLeftTopRightCornerRadius:(float)radius{
        
        //设置所需的圆角位置以及大小
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(radius, radius)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
        
    }
    
    // 参数可选值
    typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
        UIRectCornerTopLeft     = 1 << 0,
        UIRectCornerTopRight    = 1 << 1,
        UIRectCornerBottomLeft  = 1 << 2,
        UIRectCornerBottomRight = 1 << 3,
        UIRectCornerAllCorners  = ~0UL
    };
    

    使用绘图技术 直接给image切圆角

     - (UIImage *)imageWithCornerRadius:(CGFloat)radius size:(CGSize)size {
        CGRect rect = (CGRect){0.f, 0.f, size};
        UIGraphicsBeginImageContextWithOptions(size, NO, UIScreen.mainScreen.scale);
        CGContextAddPath(UIGraphicsGetCurrentContext(), [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius].CGPath);
            CGContextClip(UIGraphicsGetCurrentContext());
        [self drawInRect:rect];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        return image;
    }
    

    相关文章

      网友评论

          本文标题:iOS-防止离屏渲染

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