drawRect中抗鋸齒

在開始以前,咱們須要建立一個DrawRectViewspa

 

其初始代碼爲3d

//
//  DrawRectView.h
//  CGContextSetShouldAntialias
//
//  Created by YouXianMing on 2017/8/30.
//  Copyright © 2017年 TechCode. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface DrawRectView : UIView

@end
//
//  DrawRectView.m
//  CGContextSetShouldAntialias
//
//  Created by YouXianMing on 2017/8/30.
//  Copyright © 2017年 TechCode. All rights reserved.
//

#import "DrawRectView.h"

@implementation DrawRectView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor   = [UIColor clearColor];
        self.layer.borderWidth = 0.5f;
        self.layer.borderColor = [UIColor redColor].CGColor;
    }
    
    return self;
}

@end

 

在ViewController中使用(尺寸爲100x100並居中)code

 

顯示效果以下(用紅色邊框顯示邊界)blog

 

修改DrawRectView.m代碼以下圖片

//
//  DrawRectView.m
//  CGContextSetShouldAntialias
//
//  Created by YouXianMing on 2017/8/30.
//  Copyright © 2017年 TechCode. All rights reserved.
//

#import "DrawRectView.h"

@implementation DrawRectView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor   = [UIColor clearColor];
        self.layer.borderWidth = 0.5f;
        self.layer.borderColor = [UIColor redColor].CGColor;
    }
    
    return self;
}

- (void)drawRect:(CGRect)rect {
    
    // Set stroke color
    [[UIColor blackColor] setStroke];
    
    // Draw 7 lines.
    for (int i = 0; i < 7; i++) {
        
        UIBezierPath *path = [UIBezierPath bezierPath];
        path.lineWidth     = 0.5f;
        [path moveToPoint:CGPointMake(10, 10 + i * 10.3)];
        [path addLineToPoint:CGPointMake(10 + 80, 10 + i * 10.3)];
        [path stroke];
    }
}

@end

 

其實就添加了下面的繪圖代碼而已,繪製7條線條,每條線條的寬度爲0.5it

 

效果以下io

 

將圖片放大後會發現,線條的寬度並不一致,有的顏色深,有的顏色淺,這就是開了抗鋸齒以後的效果class

 

修改代碼關閉抗鋸齒import

//
//  DrawRectView.m
//  CGContextSetShouldAntialias
//
//  Created by YouXianMing on 2017/8/30.
//  Copyright © 2017年 TechCode. All rights reserved.
//

#import "DrawRectView.h"

@implementation DrawRectView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor   = [UIColor clearColor];
        self.layer.borderWidth = 0.5f;
        self.layer.borderColor = [UIColor redColor].CGColor;
    }
    
    return self;
}

- (void)drawRect:(CGRect)rect {
    
    // Get context.
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // Sets anti-aliasing on.
    CGContextSetShouldAntialias(context, NO);
    
    // Set stroke color
    [[UIColor blackColor] setStroke];
    
    // Draw 7 lines.
    for (int i = 0; i < 7; i++) {
        
        UIBezierPath *path = [UIBezierPath bezierPath];
        path.lineWidth     = 0.5f;
        [path moveToPoint:CGPointMake(10, 10 + i * 10.3)];
        [path addLineToPoint:CGPointMake(10 + 80, 10 + i * 10.3)];
        [path stroke];
    }
}

@end

 

顯示效果bfc

 

圖片放大後,線條寬度一致

 

 

結論

開了抗鋸齒後,系統會對繪製的線條進行必定的模糊處理,來達到不容易看到狗牙的目的,什麼是狗牙?你能夠運行如下代碼來看看二者之間的區別

//
//  DrawRectView.m
//  CGContextSetShouldAntialias
//
//  Created by YouXianMing on 2017/8/30.
//  Copyright © 2017年 TechCode. All rights reserved.
//

#import "DrawRectView.h"

@implementation DrawRectView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor   = [UIColor clearColor];
        self.layer.borderWidth = 0.5f;
        self.layer.borderColor = [UIColor redColor].CGColor;
    }
    
    return self;
}

- (void)drawRect:(CGRect)rect {
    
    // Get context.
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // Sets anti-aliasing off.
    CGContextSetShouldAntialias(context, NO);
    
    // Set stroke color
    [[UIColor blackColor] setStroke];
    
    // Draw 7 lines.
    for (int i = 0; i < 7; i++) {
        
        UIBezierPath *path = [UIBezierPath bezierPath];
        path.lineWidth     = 0.5f;
        [path moveToPoint:CGPointMake(0, 0 + i * 10.3)];
        [path addLineToPoint:CGPointMake(10 + 80, 10 + i * 10.3)];
        [path stroke];
    }
}

@end

 

關閉抗鋸齒後不會出現模糊現象,都會出現鋸齒,俗稱狗牙

 

 

打開抗鋸齒功能以後線條會模糊,鋸齒獲得了一些緩解,稱做抗鋸齒

相關文章
相關標籤/搜索