自定义三角形 UICollectionviewCell 在 collectionview [英] Custom Triangular UICollectionviewCell In collectionview

查看:26
本文介绍了自定义三角形 UICollectionviewCell 在 collectionview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Normally UICollectionviewcell is of rectangular box shaped and we can modify the appearance in custom UICollectionViewCell , But in my case i want the cell to be of shape triangular or any shape other than simple rectangular, How can i achieve this function?

推荐答案

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


}

viewController 中的代码.

Code in viewController.

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

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

其中 boole 是控制器中的简单布尔变量.

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
        }



    }


}

这篇关于自定义三角形 UICollectionviewCell 在 collectionview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆