gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:57:00 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com