2016-10-12 3 views
1

Я реализую как макет календаря с некоторой модификацией, которая показана на скриншоте. Для этого я использовал UICollectionView. Проблема в том, что мне нужно нарисовать непрерывную линию ширины экрана (зеленая линия на снимке экрана). The green line should cover the whole width, я знаю, что он не показывает круговую ячейку из-за half of the cornerRadius и вертикальной линии только после первой ячейки (10 часов утра). Там, где я должен добавить фигурный слой, чтобы он выглядел как сплошная линия. Вот код, который я пробовал до сих пор.Добавление горизонтальной линии под ячейкой в ​​uicollectionview

KKViewController.m

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! KKBookCollectionViewCell 

    self.bookingCollectionView.backgroundColor = UIColor.whiteColor() 

    let rectangularRowIndex:NSInteger = indexPath.row % 5 
    if(rectangularRowIndex == 0) 
    { 
     cell.userInteractionEnabled = false 
     cell.layer.cornerRadius = 0 
     cell.timeSlotLabel.text = "10am" 
     cell.backgroundColor = UIColor.whiteColor() 
     cell.layer.borderWidth = 0 
     cell.layer.borderColor = UIColor.clearColor().CGColor 

    } 
    else 
    { 
     cell.userInteractionEnabled = true 
     cell.layer.cornerRadius = cell.frame.size.width/2 
     cell.timeSlotLabel.text = "" 
     //cell.backgroundColor = UIColor.lightGrayColor() 
     cell.layer.borderWidth = 1 
     cell.layer.borderColor = UIColor.grayColor().CGColor 

     if cell.selected == true 
     { 
      cell.backgroundColor = UIColor.greenColor() 
     } 

     else 
     { 
      cell.backgroundColor = UIColor.lightGrayColor() 
     } 
    } 
    return cell 
} 

KKCollectionCell.m

var borderWidth:CGFloat! 
var borderPath:UIBezierPath! 

override func awakeFromNib() { 
    super.awakeFromNib() 
    drawHorizontalLine() 
    dottedLine(with: borderPath, and: borderWidth) 

    drawVerticalLine() 
    dottedLine(with: borderPath, and: borderWidth) 

func drawVerticalLine() 
{ 
    borderPath = UIBezierPath() 
    borderPath.moveToPoint(CGPointMake(self.frame.origin.x + self.frame.size.width, self.frame.origin.y)) 
    //borderPath.addLineToPoint(CGPointMake(self.frame.size.width - 5, self.frame.origin.y + self.frame.size.height - 50)) 

    borderPath.addLineToPoint(CGPointMake(self.frame.origin.x + self.frame.size.width, self.frame.origin.y + self.frame.size.height)) 
    borderWidth = 2.0 
    print("border path is %f, %f:\(borderPath)") 
} 

func drawHorizontalLine() 
{ 
    borderPath = UIBezierPath() 
    borderPath.moveToPoint(CGPointMake(0, self.frame.origin.y)) 
    borderPath.addLineToPoint(CGPointMake(self.frame.size.width + 10, self.frame.origin.y)) 
    borderWidth = 2.0 
    print("border path is %f, %f:\(borderPath)") 
} 

func dottedLine (with path:UIBezierPath, and borderWidth:CGFloat) 
{ 
    let shapeLayer = CAShapeLayer() 
    shapeLayer.strokeStart = 0.0 
    shapeLayer.strokeColor = UIColor.greenColor().CGColor 
    shapeLayer.lineWidth = borderWidth 
    shapeLayer.lineJoin = kCALineJoinRound 
    shapeLayer.lineDashPattern = [1,2] 
    shapeLayer.path = path.CGPath 
    self.layer.addSublayer(shapeLayer) 
} 

enter image description here

+0

попробовать с «bringsubViewToFront» – Janmenjaya

+0

вы имеете в виду, как этот 'self.layer.bringSubviewToFront (self.layer.addSublayer (shapeLayer))' – Xcoder

+0

Добавить UIView в камеру и установить фон и cornerRadius из UIView, а не клетки сам. –

ответ

0

Вы можете добавить новый вид внутри вид коллекции ячейки и установите радиус угла для это новый взгляд. Также вам нужно уменьшить расстояние между ячейками. Тогда линия будет выглядеть так, как вы ожидали.

+0

Это проблема, не может уменьшить пространство между ячейками – Xcoder

+0

Вы уменьшили минимальный интервал? –