• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 在collectionview中自定义三角形UICollectionviewCell

[复制链接]
菜鸟教程小白 发表于 2022-12-12 11:04:59 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

通常 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap