2
通常UICollectionviewcell是矩形框形狀,我們可以修改自定義UICollectionViewCell中的外觀,但在我的情況下,我希望單元格的形狀爲三角形或除簡單矩形以外的任何形狀,我如何實現這個功能?自定義三角UICollectionviewCell在collectionview
通常UICollectionviewcell是矩形框形狀,我們可以修改自定義UICollectionViewCell中的外觀,但在我的情況下,我希望單元格的形狀爲三角形或除簡單矩形以外的任何形狀,我如何實現這個功能?自定義三角UICollectionviewCell在collectionview
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
}
倒不如把這個掩蔽代碼自定義單元格和比檢測其中的感動和改變一個布爾值屬性,這是一個簡單的解決方案,是的,你可以去一個更好的。
自定義單元格中的代碼。
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是控制器中的簡單布爾變量。
自定義單元的完整代碼以供參考。
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
}
}
}
請看圖像http://i.imgur.com/RIRGCDw.png ......我也跟着你的腳步,但是當我點擊我的單元格的其餘部分是白色的我didselectitem代表被稱爲......我不應該被稱爲如果我的細胞這種切片這種方式。 –
您可以使用此函數path.containsPoint(self.clickedLocation)來檢查點是否在路徑內或外部,並且比改變boole變量的值。這將比測試x和y好得多。其他方法包括CGPath命中測試,這可能需要您使用path.containsPoint(self.clickedLocation),以便您可以檢查邊界線 –
: - 我完全同意你的意見....我們可以使用path.contain點來檢查接觸點是否在路徑內.......我只是想讓你知道我實際上想要做的是使用UIcollectionview使這種類型的視圖http://i.imgur.com/VGCQcWN.png .....請告訴我如何做到這一點.....並感謝您的回答。 –