【问题标题】:UICollectionViewCell dashed borderUICollectionViewCell 虚线边框
【发布时间】:2016-11-22 14:21:31
【问题描述】:

我想让我的自定义 UICollectionViewCell 周围有一个虚线边框,我尝试使用图层和贝塞尔路径,但没有任何方法有帮助。

【问题讨论】:

    标签: ios objective-c border uicollectionviewcell cashapelayer


    【解决方案1】:

    在你UICollectionViewCell's子类

    static CGFloat const kDashedBorderWidth     = (2.0f);
    static CGFloat const kDashedPhase           = (0.0f);
    static CGFloat const kDashedLinesLength[]   = {4.0f, 2.0f};
    static size_t const kDashedCount            = (2.0f);
    
    
    
    -(void)drawRect:(CGRect)rect
    {
    
    
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetLineWidth(context, kDashedBorderWidth);
        CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
        CGContextSetLineDash(context, kDashedPhase, kDashedLinesLength, kDashedCount) ;
        CGContextAddRect(context, rect);
        CGContextStrokePath(context);
    
    }
    

    【讨论】:

      【解决方案2】:

      在你的类中添加这个方法。

      - (CAShapeLayer *) addDashedBorderWithColor: (CGColorRef)color withSize:(CGSize)size{
          CAShapeLayer *shapeLayer = [CAShapeLayer layer];
      
          CGSize frameSize = size;
      
          CGRect shapeRect = CGRectMake(0.0f, 0.0f, frameSize.width, frameSize.height);
          [shapeLayer setBounds:shapeRect];
          [shapeLayer setPosition:CGPointMake( frameSize.width/2,frameSize.height/2)];
      
          [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
          [shapeLayer setStrokeColor:color];
          [shapeLayer setLineWidth:5.0f];
          [shapeLayer setLineJoin:kCALineJoinRound];
          [shapeLayer setLineDashPattern:
           [NSArray arrayWithObjects:[NSNumber numberWithInt:10],
            [NSNumber numberWithInt:5],
            nil]];
          UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:shapeRect cornerRadius:15.0];
          [shapeLayer setPath:path.CGPath];
      
          return shapeLayer;
      }
      

      在您的cellForRowAtIndexPath 方法中写入以下代码。

      UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier"@"cell"];
          //Do what you want to set in your collectionViewCell
          CAShapeLayer *maskLayer= [self addDashedBorderWithColor:[UIColor whiteColor].CGColor withSize:cell.borderView.frame.size];
          cell.layer.mask = maskLayer;
      

      【讨论】:

      • 我在您使用的自定义单元格中没有任何属性作为边框视图。
      • 我的意思是用你自己的视图名称来改变它,你想在它上面绘制这个边框。我刚刚把我的虚拟对象寄给你了。
      猜你喜欢
      • 1970-01-01
      • 2012-06-04
      • 1970-01-01
      • 2014-12-14
      • 2017-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多