OGeek|极客世界-中国程序员成长平台

标题: ios - 在collectionview中自定义三角形UICollectionviewCell [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:04
标题: ios - 在collectionview中自定义三角形UICollectionviewCell

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



Best Answer-推荐答案


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
}

enter image description here

最好将此屏蔽代码带到自定义单元格,而不是检测触摸和更改 bool 属性的位置,这是一个简单的解决方案,是的,您可以选择更好的解决方案。

自定义单元格中的代码。

   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
    }
}


}

viewController 中的代码。

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

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

其中 boole 是 Controller 中的一个简单 bool 变量。

自定义单元格的完整代码供引用。

 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
        }



    }


}

关于ios - 在collectionview中自定义三角形UICollectionviewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36871544/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4