ios - 尝试在手势识别时翻转单元格上的 ImageView ,代码工作正常但没有翻转

标签 ios xcode swift2 uicollectionview uigesturerecognizer

我有一个 Collection View ,在 Collection View 上,我使用了 imageview,我在 imageview 上添加了 UISwipeGestureRecognizer,它工作正常。它进入刷卡方法。在 Swipe 方法中,我试图翻转单元格上的 imageview 但它不起作用。

下面是我的澄清代码。

// collectionview delegate method in which I have added gesture.

    internal func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        let reuseIdentifier = "cell" // also enter this string as the cell identifier in the storyboard
        index = indexPath
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell
        let imageNameString = self.items.objectAtIndex(indexPath.row) as! String

        let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swiped(_:))) // put : at the end of method name
        swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
        cell.myImageView.addGestureRecognizer(swipeLeft)

        cell.myImageView.userInteractionEnabled = true;
        cell.myImageView.image = UIImage(named: imageNameString)
        return cell;
    }

// Swiped Method called when we swipe on imageview

    func swiped(gesture: UIGestureRecognizer) {
        let reuseIdentifier = "cell" // also enter this string as the cell identifier in the storyboard

        let cell = self.myCollectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath:index ) as! MyCollectionViewCell


        if let swipeGesture = gesture as? UISwipeGestureRecognizer
        {
            switch swipeGesture.direction {

            case UISwipeGestureRecognizerDirection.Left :
                print("User swiped left")
                cell.updateCell()
                self.myCollectionView.reloadData()
            default:
                break
            }
        }
    }

当我们在单元格上滑动 imageview 但它没有翻转时,会调用更新单元格方法。如果我直接在 UICollectionViewCell 中纠正这段代码,它就可以正常工作。我已经为 uicollectionviewcell

定制了类(class)
func updateCell()
{
UIView.transitionWithView(self.contentView, duration: 0.6, options: UIViewAnimationOptions.TransitionFlipFromLeft, animations: {
            self.contentView.insertSubview(self.myImageView, aboveSubview: self.myImageView)
            self.myImageView.image = UIImage (named: "wallpaper2")      
            }, completion: nil)

    }

如有任何帮助,我们将不胜感激。哪里错了?

最佳答案

终于找到解决办法了——

//替换代码-

func swiped(gesture: UIGestureRecognizer)
 {
    let reuseIdentifier = "cell" // also enter this string as the cell identifier in the storyboard

    let cell = self.myCollectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath:index ) as! MyCollectionViewCell

    ....
   }

//使用下面的代码 -

func swiped(gesture: UIGestureRecognizer) {

    let cell = self.myCollectionView.cellForItemAtIndexPath(index) as! MyCollectionViewCell
      .....
   }

希望对遇到同样问题的 friend 有所帮助 感谢以上所有试图帮助找出解决方案的人。

关于ios - 尝试在手势识别时翻转单元格上的 ImageView ,代码工作正常但没有翻转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41222552/

相关文章:

ios - 核心数据中的 True/False bool 值

ios - 将标记项正确添加到 nsmutable 数组中

iphone - 如何给这个选择器 View 一个数据源

ios - Git - Xcode 文件计划在 Project Navigator 'D' 中删除

ios - 删除记录时出错,由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException'

swift2 - 显示/隐藏状态栏导致 View 跳转

ios - prapareForSegue 无法为目标 Controller 设置任何属性

iphone - 检查 rootviewcontroller 的类型

swift - SWreveal - 如何返回到我开始使用的 View Controller

xcode - Swift - 表达式类型在没有更多上下文的情况下不明确