iOS ~ 用drawRect 画表格,或YYAsyncLaye

2021-11-11  本文已影响0人  阳光下的叶子呵

加载滑动tableviewCell时,内容太多, 界面会卡顿,那么用到异步渲染

1、YYAsyncLayer

2、YYAsyncLayer 的异步绘制之道

3、github库强大的YYText

【YYLabel】:异步显示,设置yyLabel.displaysAsynchronously=YES;

4、下面的代码是drawRect方法:(非YYAsyncLayer)

IOS 用drawRect 画表格
自定义一个View DrawLine
 
DrawLine.h
 
#import <UIKit/UIKit.h>
 
@protocol gridTouchDelete <NSObject>
 
- (void)gridTouchColumn:(NSInteger)column touchRow:(NSInteger)row;
 
@end
 
@interface DrawLine : UIView
 
@property(nonatomic, assign) id<gridTouchDelete>delegate;
 
@end
 
#import "DrawLine.h"
 
#define GRID_WIDTH 44
#define GRID_HEIGHT 44
 
@implementation DrawLine
 
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
 
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
 
    // 格子线条颜色
    CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
    CGContextBeginPath(context);
 
    NSInteger numRows = ;
    float gridHeight = numRows*(GRID_HEIGHT+)+;
 
    // 背景色
    CGRect rectangleGrid = CGRectMake(,,self.frame.size.width,gridHeight);
    CGContextAddRect(context, rectangleGrid);
    CGContextSetFillColorWithColor(context, [[UIColor grayColor] colorWithAlphaComponent:0.2].CGColor);
    CGContextFillPath(context);
 
    for (int i = ; i < ; i++) {
        //columns 列
        CGContextMoveToPoint(context, i*(GRID_WIDTH+)+i*, );
        CGContextAddLineToPoint(context, i*(GRID_WIDTH+)+i*, gridHeight);
 
        if (i > numRows) continue;
 
        //rows 行
        CGContextMoveToPoint(context, , i*(GRID_HEIGHT+)+i*+);
        CGContextAddLineToPoint(context, self.frame.size.width, +i*(GRID_HEIGHT+)+i*+);
 
    }
 
    CGContextStrokePath(context);
    CGContextSetAllowsAntialiasing(context, YES);
 
    NSInteger gridNum = numRows * ;
    for (int i = ; i < gridNum; i ++) {
        int targetColumn = i%;
        int targetRow = i/;
        int targetX = targetColumn * (GRID_WIDTH+)+;
        int targetY =  targetRow * (GRID_HEIGHT+)+;
 
         NSString *gridStr = [NSString stringWithFormat:@"%d",i+];
 
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0) {
            UIColor *fontColor;
            if (targetColumn ==  || targetColumn == ) {
                fontColor = [UIColor redColor];
            }
            else
            {
                fontColor = [UIColor blackColor];
            }
 
            [gridStr drawInRect:CGRectMake(targetX, targetY, self.frame.size.width, GRID_WIDTH) withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Bold" size:],NSForegroundColorAttributeName:fontColor}];
        }
        else
        {
            [gridStr drawInRect:CGRectMake(targetX, targetY, self.frame.size.width, GRID_WIDTH) withFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:] lineBreakMode:NSLineBreakByClipping alignment:NSTextAlignmentCenter];
 
            CGContextSetFillColorWithColor(context,[UIColor redColor].CGColor);
        }
 
    }
}
 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
 
    float xLocation = touchPoint.x;
    float yLocation = touchPoint.y;
 
    int column = floorf(xLocation/(GRID_HEIGHT+));
    int row = floorf(yLocation/(GRID_WIDTH+));
 
    if ([_delegate respondsToSelector:@selector(gridTouchColumn:touchRow:)]) {
        [_delegate gridTouchColumn:column touchRow:row];
    }
}
 
MainViewController
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
 
    DrawLine *lineView = [[DrawLine alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width, )];
    lineView.backgroundColor = [UIColor clearColor];
    lineView.delegate = self;
    [self.view addSubview:lineView];
}
 
- (void)gridTouchColumn:(NSInteger)column touchRow:(NSInteger)row
{
    NSLog(@"行:%d,列:%d",column,row);
}
上一篇下一篇

猜你喜欢

热点阅读