彩票双色球走势图

走势图是彩票中综合多期开奖结果得出每个投注号码的遗漏值、出现次数、平均遗漏、最大遗漏等值然后展示的表格视图。这里只做了双色球蓝球的走势图,其它彩种的走势图与此类似。

先看效果图:


trend.gif

布局

布局分三部分:
1.顶部号码区 -- 使用UIScrollView
2.左侧期号区 -- 使用UITableView
3.右侧号码区 -- 使用UICollectionView

细节处理

1.滑动时需要使三大块在 x/y 轴偏移量保持一致

可以在 scrollViewDidScroll 中进行控制

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGPoint offset = scrollView.contentOffset;
    
    if (scrollView == self.issueList) { // 左
        self.numberList.contentOffset = CGPointMake(self.numberList.contentOffset.x, offset.y);
    }else if (scrollView == self.numberList){ // 右
        self.issueList.contentOffset = CGPointMake(self.issueList.contentOffset.x, offset.y);
        self.ballsPagView.contentOffset = CGPointMake(self.numberList.contentOffset.x, self.ballsPagView.contentOffset.y);
    }else if (scrollView == self.ballsPagView){ // 上
        self.numberList.contentOffset = CGPointMake(self.ballsPagView.contentOffset.x, self.numberList.contentOffset.y);
    }
}
2.右侧号码区布局

右侧 UICollectionViewcontentSize 已经超出了默认布局的范围,而且默认布局也不到达到图示中双向滑动的效果,所以这里使用 layout自定义布局。
创建继承自UICollectionViewLayoutSSQTrendLayout类进行自定义布局。

#import "SSQTrendLayout.h"

@implementation SSQTrendLayout{
    
    NSMutableArray *_attributes;
    NSInteger _itemCount;
}

- (void)prepareLayout{
    [super prepareLayout];
    
    _itemCount = [self.collectionView numberOfSections];
    _attributes = [NSMutableArray array];
    
    for (int index = 0; index < _itemCount; index ++) {
        NSInteger pSec = [self.collectionView numberOfItemsInSection:index];
        for (int i = 0; i < pSec; i ++) {
            
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:index];
            UICollectionViewLayoutAttributes *pAttri = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
            
            CGFloat x = 0;
            CGFloat y = 0;
            
            // 每个item的宽高
            x = i * (1 + kItemSize.width);
            y = 1 + index * (1 + kItemSize.height);
            pAttri.frame = CGRectMake(x, y, kItemSize.width, kItemSize.height);
            
            [_attributes addObject:pAttri];
        }
    } 
}

// 设置内容区域的大小
- (CGSize)collectionViewContentSize{
    // sections
    NSInteger sec = [self.collectionView numberOfSections];
    return CGSizeMake((self.cols + 1) * 1 + self.cols * kItemSize.width, sec * (kItemSize.height + 1));
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
    return _attributes;
}

@end
3.右侧号码区cell处理

通常文本的显示用UILabel就可以了,但这里由于页面内cell数量众多,并且部分cell上还需要圆形背景,使用UILabel会造成明显的卡顿,这里直接在cell上绘制所需的背景和文字。

- (void)drawRect:(CGRect)rect{
    
    // 创建画布
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 设置填充色
    CGContextSetFillColorWithColor(context, self.bgColor.CGColor);
    // 圆形直径
    CGFloat w = MIN(kItemSize.width, kItemSize.height) - 4;
    // 园点坐标
    CGPoint center = CGPointMake(rect.origin.x + rect.size.width / 2.0,
                                 rect.origin.y + rect.size.height / 2.0);
    
    if (self.shapeType == ShapeWithBackgroundViewRound) { // 圆形背景
        
        CGContextAddArc(context, center.x, center.y, w / 2.0, 0 , 2 * M_PI, 0);
        CGContextDrawPath(context, kCGPathFill);
        [self drawShowText];
    }else if (self.shapeType == ShapeWithNoBackgroundView){ // 无背景
        [self drawShowText];
    }
}

// 绘制文字
- (void)drawShowText{
    
    UIFont *font = [UIFont systemFontOfSize:12];
    CGSize textSize = [self.number sizeWithAttributes:@{NSFontAttributeName:font}];
    CGRect textRect = CGRectMake((self.frame.size.width - textSize.width) / 2.0, (self.frame.size.height - textSize.height) / 2.0, self.frame.size.width, self.frame.size.height);
    [self.number drawInRect:textRect withAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:self.textColor}];
}

考虑到cell的重用特性,需要在UICollectionView代理方法获取cell时重新绘制:

[cell setNeedsDisplay];
return cell;
4.开奖号码连接线

开奖号码连接线需要直接添加于UICollectionView的layer图层上,以保证线段与列表cell相对位置的正确,而线段可以采用 UIBezierPathCAShapeLayer 结合的方式绘制。

// 绘制折线
- (void)drawBrokenLines{
    
    // 计算线段开始点与结束点
    NSMutableArray <TrendPerBall *> *ballsPoints = [NSMutableArray array];
    for (int sec = 0; sec < self.listSource.count; sec ++) {
        SSQTrendModel *tm = self.listSource[sec];
        NSArray *showBalls = tm.trends.lastObject;
        for (int row = 0; row < showBalls.count; row ++) {
            
            TrendPerBall *pb = showBalls[row];
            CGFloat x = 0;
            CGFloat y = 0;
            
            // 每个item的宽高,需要与layout中item的frame保持一致
            x = row * (1 + kItemSize.width);
            y = 1 + sec * (1 + kItemSize.height);
            CGRect pFrame = CGRectMake(x, y, kItemSize.width, kItemSize.height);
            CGPoint pCenter = CGPointMake(pFrame.origin.x + pFrame.size.width / 2.0,
                                          pFrame.origin.y + pFrame.size.height / 2.0);
            
            pb.realRect = pFrame;
            pb.center = pCenter;
            
            if (pb.isAwardNumber) {
                [ballsPoints addObject:pb];
            }
        }
    }
    
    // 节点圆形半径
    CGFloat r = (MIN(kItemSize.width, kItemSize.height) - 4) / 2.0;
    // 计算绘制线段的开始点和结束点
    for (int index = 0; index < ballsPoints.count - 1; index ++) {
        
        TrendPerBall *fm = ballsPoints[index];
        TrendPerBall *tm = ballsPoints[index + 1];
        // 节点间距
        CGFloat dis = sqrt(pow(tm.center.x - fm.center.x, 2.0) + pow(tm.center.y - fm.center.y, 2.0));
        if (tm.center.x <= fm.center.x) { // 下个节点位于第上个节点左侧
            
            fm.startPoint = CGPointMake(fm.center.x - r * ((fm.center.x - tm.center.x) / dis),
                                        fm.center.y + r * ((tm.center.y - fm.center.y) / dis));
            
            tm.endPoint   = CGPointMake(tm.center.x + r * ((fm.center.x - tm.center.x) / dis),
                                        tm.center.y - r * ((tm.center.y - fm.center.y) / dis));
            
        }else{ // 下个节点位于第上个节点右侧
            
            fm.startPoint = CGPointMake(fm.center.x + r * ((tm.center.x - fm.center.x) / dis),
                                        fm.center.y + r * ((tm.center.y - fm.center.y) / dis));
            
            tm.endPoint   = CGPointMake(tm.center.x - r * ((tm.center.x - fm.center.x) / dis),
                                        tm.center.y - r * ((tm.center.y - fm.center.y) / dis));
        }
        
        UIBezierPath *path = [[UIBezierPath alloc] init];
        [path moveToPoint:fm.startPoint];
        [path addLineToPoint:tm.endPoint];
        
        CAShapeLayer *layer = [CAShapeLayer layer];
        layer.path = path.CGPath;
        layer.strokeColor = kBLUE_COLOR.CGColor;
        layer.fillColor = [UIColor clearColor].CGColor;
        layer.lineWidth = 1;
        layer.lineCap = kCALineJoinRound;
        layer.lineJoin = kCALineJoinRound;
        
        [self.numberList.layer addSublayer:layer];
    }
}

以上即为走势图中较重要的部分,完整的Demo地址:https://github.com/HuberyYang/LotteryTrend.git
如果本篇文章能帮到您,请您随手丢个star

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 119,054评论 1 241
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 51,980评论 1 200
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 74,402评论 0 167
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 36,473评论 0 127
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 43,290评论 1 206
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 35,928评论 1 126
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 27,913评论 2 207
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 27,039评论 0 120
  • 想象着我的养父在大火中拼命挣扎,窒息,最后皮肤化为焦炭。我心中就已经是抑制不住地欢快,这就叫做以其人之道,还治其人...
    爱写小说的胖达阅读 26,005评论 5 173
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 30,108评论 0 178
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 27,307评论 1 170
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 28,560评论 1 178
  • 白月光回国,霸总把我这个替身辞退。还一脸阴沉的警告我。[不要出现在思思面前, 不然我有一百种方法让你生不如死。]我...
    爱写小说的胖达阅读 22,865评论 0 25
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 25,485评论 2 165
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 29,341评论 3 173
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 24,325评论 0 4
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 24,351评论 0 113
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 30,542评论 2 189
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 30,958评论 2 188

推荐阅读更多精彩内容