如何在swift中的集合视图单元格内使用点击手势

时间:2015-09-29 12:07:02

标签: ios swift collectionview

我有一个集合视图单元格,其中包含图像视图。我希望有一个场景,双击图像在完整图像视图上打开它,再次双击从该图像视图中删除该完整图像。

 var a = ["1.png","2.png"]

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView!.dataSource = self
        collectionView!.delegate = self
        collectionView!.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
        collectionView!.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(collectionView!)


        var gestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapImageView:")
        self.imageview.addGestureRecognizer(gestureRecognizer)
        gestureRecognizer.numberOfTapsRequired = 1



    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        var cell = collectionView.cellForItemAtIndexPath(indexPath) as CollectionViewCell
        if((cell.selected) && (indexPath.row%2 == 0))
        {
//            cell.imageView?.image = UIImage( named: a[indexPath.row])
            cell.backgroundColor = UIColor.greenColor()
         //  imageview.image = UIImage( named: a[indexPath.row])
            btn_close.hidden = false
            let aSelector : Selector = "start:"
            let tapGesture = UITapGestureRecognizer(target: self, action: aSelector)
            let tap1 = UIGestureRecognizer(target: self, action: aSelector)
            tapGesture.numberOfTapsRequired = 2
            cell.addGestureRecognizer(tapGesture)


//            var gestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tapImageView:")
//            self.imageview.addGestureRecognizer(gestureRecognizer)
//            gestureRecognizer.numberOfTapsRequired = 2


        }
        else if((cell.selected) && (indexPath.row%2 != 0))
        {
            cell.backgroundColor = UIColor.redColor()
         //  imageview.image = UIImage( named: a[indexPath.row])
            btn_close.hidden = false

        }
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        var abc : CGSize!

        if(a.count == 1){
            abc = CGSize(width: self.view.frame.width, height: self.view.frame.height/2)
        }
        else if(a.count == 2) {
            abc = CGSize(width: self.view.frame.width/2, height: self.view.frame.height/2)
        }
        else if (a.count == 3) || (a.count==5){
            abc = CGSize(width: self.view.frame.width/3, height: self.view.frame.height/2)
        }
        else if(a.count == 4) {
            abc = CGSize(width: self.view.frame.width/2, height: self.view.frame.height/5)
        }
        else if (a.count>=5) && (a.count<9){
            abc = CGSize(width: self.view.frame.width/3.05, height: self.view.frame.height/4.75)
        }
        else if (a.count>=9){
            abc = CGSize(width: self.view.frame.width/3, height: self.view.frame.height/9)
        }
             return abc
    }

0 个答案:

没有答案