简体   繁体   中英

Custom Triangular UICollectionviewCell In collectionview

通常,UICollectionviewcell是矩形框形状的,我们可以在自定义UICollectionViewCell中修改外观,但是在我的情况下,我希望单元格的形状为三角形或除简单矩形之外的其他任何形状,我如何实现此功能?

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
    var path = UIBezierPath();
    var mask = CAShapeLayer();




            path.moveToPoint(CGPoint(x: 0,y: 0))
            path.addLineToPoint(CGPoint(x: cell.bounds.size.width-(cell.bounds.size.width/2), y:cell.bounds.size.width-(cell.bounds.size.width/2) ))
            path.addLineToPoint(CGPoint(x: cell.bounds.size.width, y: 0))
            path.addLineToPoint(CGPoint(x: 0, y: 0))

            mask.frame = cell.bounds
            mask.path = path.CGPath
            cell.layer.mask = mask




     cell.backgroundColor = UIColor.redColor()
    return cell
}

在此处输入图片说明

It would be better to bring this masking code to custom cell and than detect where touched and changed a boolean property this is a simple solution ,yes you can go for a better one.

Code in custom cell .

   override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
     super.touchesBegan(touches, withEvent: event)
         let touch : UITouch! = touches.first

        self.clickedLocation = touch.locationInView(touch.view)

        print(self.clickedLocation.x)
        print(self.clickedLocation.y)

  //put condition on x and y here  and get controller and change boolean property .
    if self.clickedLocation.y < 100
    {

    ( ( UIApplication.sharedApplication().keyWindow?.rootViewController) as? collectionViewController )?.boole = true
    }
    else
    {
         ( ( UIApplication.sharedApplication().keyWindow?.rootViewController) as? collectionViewController )?.boole = false
    }
}


}

Code in viewController.

 override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        if boole == false{
            print("hello")
        }
        else{
            print("bello")
        }
    }

where boole is a simple Boolean variable in controller.

complete code for custom cell for reference .

 import UIKit

    class CollectionViewCell: UICollectionViewCell {
        var clickedLocation = CGPoint()
        var path : UIBezierPath!
        var mask : CAShapeLayer!

        override func drawRect(rect: CGRect) {
            super.drawRect(rect)

           path = UIBezierPath();
             mask = CAShapeLayer();

            path!.moveToPoint(CGPoint(x: 0,y: 0))
            path!.addLineToPoint(CGPoint(x: self.bounds.size.width-(self.bounds.size.width/2), y:self.bounds.size.width-(self.bounds.size.width/2) ))
            path!.addLineToPoint(CGPoint(x: self.bounds.size.width, y: 0))
            path!.addLineToPoint(CGPoint(x: 0, y: 0))

            mask!.frame = self.bounds
            mask!.path = path!.CGPath
            self.layer.mask = mask


        }
        override func awakeFromNib() {
            super.awakeFr

omNib()
        // Initialization code
    }
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
     super.touchesBegan(touches, withEvent: event)
         let touch : UITouch! = touches.first

        self.clickedLocation = touch.locationInView(touch.view)

          if        path.containsPoint(self.clickedLocation)
         {
              ( ( UIApplication.sharedApplication().keyWindow?.rootViewController) as? collectionViewController )?.boole = true
        }
          else{
              ( ( UIApplication.sharedApplication().keyWindow?.rootViewController) as? collectionViewController )?.boole = false
        }



    }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM