答えて

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

触れたと、これは簡単な解決策であるブールプロパティを変更する場合は、はいあなたがより良い1のために行くことができ、カスタムセルに、このマスキングコードを持参した方がよいと検出よります。

カスタムセルのコード。

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 
     } 



    } 


} 
+0

画像http://i.imgur.com/RIRGCDw.pngを見てください......私はあなたのステップに従っしかし、私は私のdidselectitemデリゲート白である私のセルの残りの部分をクリックすると、私の細胞がこのようにスライスされたら私は呼び出されるはずがない......と呼ばれています。 –

+0

この関数path.containsPoint(self.clickedLocation)を使用して、ポイントがパス内か外かをチェックし、boole変数の値を変更することができます。これは、xとyをテストするよりもはるかに優れています。他の方法では、path.containsPoint(self.clickedLocation)を使用する場合に必要なCGPathのヒットテストが含まれているため、境界線 –

+0

を確認できます。 ....私たちはpath.containポイントを使用して、タッチされたポイントがパスの内側にあるかどうかをチェックすることができます。.......私は実際に何をしようとしているのか知りたいのですが、UIcollectionview http://i.imgur.com/VGCQcWN.png .....これを達成する方法を教えてください.....そしてあなたの答えに感謝します。 –