iOS绘制虚线框

作者: 华子1889 | 来源:发表于2016-05-31 09:26 被阅读0次

    本示例为在tableview底部添加一个虚线框,方法亦可用于其他场景。

    - (void)setUpSectionView{
        sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 110)];
        sectionView.backgroundColor = [UIColor clearColor];
    
        UIImageView* footerBackView = [[UIImageView alloc]init];
        footerBackView.frame = CGRectMake(10, 10, kSCREEN_WIDTH - 20, 100);
        footerBackView.backgroundColor = [UIColor whiteColor];
        [sectionView addSubview:footerBackView];
    
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        [shapeLayer setBounds:sectionView.bounds];
        [shapeLayer setPosition:sectionView.center];
        [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
    
        // 设置虚线颜色为blackColor
        [shapeLayer setStrokeColor:[[UIColor blackColor] CGColor]];
        [shapeLayer setStrokeColor:[HMMainlColor CGColor]];
    
        // 3.0f设置虚线的宽度
        [shapeLayer setLineWidth:1.0f];
        [shapeLayer setLineJoin:kCALineJoinRound];
    
        // 3=线的宽度 1=每条线的间距
        [shapeLayer setLineDashPattern:
         [NSArray arrayWithObjects:[NSNumber numberWithInt:5],
          [NSNumber numberWithInt:3],nil]];
    
        // Setup the path
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, 10, 10);
        CGPathAddLineToPoint(path, NULL, kSCREEN_WIDTH - 10,10);
        CGPathMoveToPoint(path, NULL, kSCREEN_WIDTH - 10, 110);
        CGPathAddLineToPoint(path, NULL, 10, 110);
        CGPathMoveToPoint(path, NULL, 10, 10);
        CGPathAddLineToPoint(path, NULL, 10, 110);
        CGPathMoveToPoint(path, NULL, kSCREEN_WIDTH - 10, 10);
        CGPathAddLineToPoint(path, NULL, kSCREEN_WIDTH - 10, 110);
    
        [shapeLayer setPath:path];
        CGPathRelease(path);
    
        // 可以把self改成任何你想要的UIView, 下图演示就是放到UITableViewCell中的
        [[sectionView layer] addSublayer:shapeLayer];
    
        addButton = [[UIButton alloc]init];
        [sectionView addSubview:addButton];
        [addButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(footerBackView.mas_left);
            make.top.mas_equalTo(footerBackView.mas_top);
            make.right.mas_equalTo(footerBackView.mas_right);
            make.height.mas_equalTo(footerBackView.mas_height);
        }];
        addButton.pr_acceptEventInterval = 2;
        addButton.titleLabel.font = HMFontSize(16);
        [addButton setImage:[UIImage imageNamed:@"my_add"] forState:UIControlStateNormal];
        [addButton setTitle:@"  添加XXX" forState:UIControlStateNormal];
        [addButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [addButton addTarget:self action:@selector(addButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        self.myTableView.tableFooterView = sectionView;
        
    }
    

    相关文章

      网友评论

        本文标题:iOS绘制虚线框

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