Reputation: 924
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?
Upvotes: 2
Views: 920
Reputation: 353
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
}
}
}
Code in viewController.
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if boole == false{
print("hello")
}
else{
print("bello")
}
}
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
}
}
}
Upvotes: 3