美文网首页
UIBezierPath+CGContextRef+NSAttr

UIBezierPath+CGContextRef+NSAttr

作者: 952625a28d0d | 来源:发表于2016-06-30 14:37 被阅读514次
#import "CurveView.h"

@implementation CurveView {
    NSArray<NSValue*> * points; // sin曲线点数组
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}

-(void)setUp {
    // 取消背景颜色
    self.backgroundColor = nil;
    self.opaque = NO;
    // 旋转屏幕重新绘制
    self.contentMode = UIViewContentModeRedraw;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setUp];
    }
    return self;
}

#pragma mark -- 设置线框颜色
-(void)setLineColor:(UIColor *)lineColor {
    _lineColor = lineColor;
    [self setNeedsDisplay];
}

#pragma mark -- 设置曲线比例大小
-(void)setScaleFactor:(CGFloat)scaleFactor {
    _scaleFactor = scaleFactor;
    [self createCurve];
    [self setNeedsDisplay];
}

#pragma mark -- 取长方形短边
-(CGFloat) minLengthOfBound {
    return MIN(self.bounds.size.width, self.bounds.size.height);
}

#pragma mark -- 绘制文字
-(void) drawParamter {
    
    UIFont * paramFont = [UIFont systemFontOfSize:[self minLengthOfBound] / 20];
    
    NSAttributedString *paramText = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"scale = %.2f", self.scaleFactor]
                                                                    attributes:@{NSFontAttributeName : paramFont,
                                                                                 NSForegroundColorAttributeName : [UIColor redColor]}];
    [paramText drawAtPoint: CGPointMake([self minLengthOfBound] / 15, [self minLengthOfBound] / 15)];
}

#pragma mark 创建点
-(void) createCurve {
    NSMutableArray<NSValue*> * a = [NSMutableArray array];
    for (CGFloat x = -[self minLengthOfBound]; x <= [self minLengthOfBound] ; ++x) {
        CGFloat r = x * M_PI / 180;
        CGFloat y = self.scaleFactor * sin(2 * r);
        CGPoint pt = CGPointMake(x, y);
        [a addObject:[NSValue valueWithCGPoint:pt]];
    }
    points = a;
}

#pragma mark -- 绘制
-(void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 长方形
    UIBezierPath * roundedRect = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:[self minLengthOfBound] / 20];
    [roundedRect addClip];
    [[UIColor whiteColor] setFill];
    UIRectFill(self.bounds);
    
    // 圆形
    UIBezierPath * round = [[UIBezierPath alloc] init];
    [round addArcWithCenter:CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2) radius:[self minLengthOfBound] / 2 startAngle:0 endAngle:M_PI * 2 clockwise:YES];
    round.lineWidth = 1.0;
    [round stroke];
    
    // 存上下文
    CGContextSaveGState(context);
    CGContextTranslateCTM(context, self.bounds.size.width / 2, self.bounds.size.height / 2);
    
    // 绘制曲线
    UIBezierPath * path = [[UIBezierPath alloc] init];
    path.lineWidth = 1.0;
    [path moveToPoint:[points[0] CGPointValue]];
    [points enumerateObjectsUsingBlock:^(NSValue * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx > 0) {
            CGPoint pt = [obj CGPointValue];
            [path addLineToPoint:pt];
        }
        
    }];
    
    // 曲线颜色
    [self.lineColor setStroke];
    [path stroke];
    
    // 释放上下文
    CGContextRestoreGState(context);
    
    // 设置文字阴影
    CGContextSetShadowWithColor(context, CGSizeMake(0, 5), 5.0, [[UIColor blackColor]CGColor]);
    [self drawParamter];
    
    // 反转文字重新绘制
    CGContextTranslateCTM(context, self.bounds.size.width,self.bounds.size.height);
    CGContextRotateCTM(context, M_PI);
    [self drawParamter];
    
}

@end```

![Paste_Image.png](https://img.haomeiwen.com/i189984/a79dc3e91054867d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

网友评论

      本文标题:UIBezierPath+CGContextRef+NSAttr

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