ios - 在collectionview中自定义三角形UICollectionviewCell
<p><p>通常 UICollectionviewcell 是矩形框形的,我们可以在自定义 UICollectionViewCell 中修改外观,但在我的情况下,我希望单元格是三角形或简单矩形以外的任何形状,我该如何实现这个功能?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><pre><code>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
}
</code></pre>
<p> <a href="/image/3nGSo.png" rel="noreferrer noopener nofollow"><img src="/image/3nGSo.png" alt="enter image description here"/></a> </p>
<p>最好将此屏蔽代码带到自定义单元格,而不是检测触摸和更改 bool 属性的位置,这是一个简单的解决方案,是的,您可以选择更好的解决方案。 </p>
<p>自定义单元格中的代码。</p>
<pre><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)
print(self.clickedLocation.x)
print(self.clickedLocation.y)
//put condition on x and y hereand 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></pre>
<p>viewController 中的代码。</p>
<pre><code> override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if boole == false{
print("hello")
}
else{
print("bello")
}
}
</code></pre>
<p>其中 boole 是 Controller 中的一个简单 bool 变量。</p>
<p>自定义单元格的完整代码供引用。</p>
<pre><code> 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
}
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 在collectionview中自定义三角形UICollectionviewCell,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36871544/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36871544/
</a>
</p>
页:
[1]