Antwort

3
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

Es wäre besser, diese Maskierung Code, um benutzerdefinierte Zelle zu bringen und als erkennen, wo eine Boolesche Eigenschaft berühren und veränderte das ist eine einfache Lösung, ja Sie für einen besseren gehen können.

Code in benutzerdefinierter Zelle.

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") 
     } 
    } 

wo Boole eine einfache Boolesche Variable in der Steuerung ist.

vollständiger Code für benutzerdefinierte Zelle als Referenz.

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 
     } 



    } 


} 
+0

Schauen Sie bitte auf das Bild http://i.imgur.com/RIRGCDw.png ...... i gefolgt Ihre Schritte, aber wenn ich den Rest Teil meiner Zelle klicken, die meine didselectitem Delegat weiß ist heißt ... was ich nicht genannt werden sollte, wenn meine Zelle so in Scheiben geschnitten ist. –

+0

Sie können diese Funktion path.containsPoint (self.clickedLocation) verwenden, um zu überprüfen, ob der Punkt innerhalb des Pfads oder außerhalb liegt und dann den Wert der boole-Variablen ändert. das wäre viel besser als das Testen von x und y. Andere Möglichkeiten beinhalten CGPath-Treffertests, die für den Fall benötigt werden, dass Sie path.containsPoint (self.clickedLocation) verwenden, damit Sie nach der Grenzlinie –

+0

suchen können: - Ich stimme absolut mit Ihnen überein .... können wir path.contain Punkte verwenden, um zu überprüfen, ob der berührte Punkt innerhalb des Pfades liegt ....... Ich möchte nur, dass Sie wissen, was ich eigentlich versuche, diese Art von View mit UIcollectionview zu machen http://i.imgur.com/VGCQcWN.png ..... bitte schlagen Sie mir vor, wie Sie das erreichen können ..... und danke für Ihre Antwort. –