【问题标题】:Trying to flip the imageview on cell on gesture recognization, code is working fine but not getting flipped尝试在手势识别时翻转单元格上的图像视图,代码工作正常但没有翻转
【发布时间】:2016-12-19 12:24:59
【问题描述】:

我有一个收藏视图,在收藏视图上,我已经采取了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上了自定义课程

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)

    }

任何帮助将不胜感激。哪里错了?

【问题讨论】:

  • 请开启uiimageview的用户交互后重试
  • @Spynet 我已经添加了 cell.myImageView.userInteractionEnabled = true;是否需要在某个地方再次添加它?
  • 一次够了,我会检查并更新你的意思,同时你也搜索......
  • 好的。我再试一次。感谢您的帮助。

标签: ios xcode swift2 uicollectionview uigesturerecognizer


【解决方案1】:

终于找到解决办法了——

//替换代码-

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
      .....
   }

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

【讨论】:

    【解决方案2】:

    如果您使用的是 uicollectionviewcell 的子类,则将手势识别器分配给 awakeFromNib 中的图像视图。还将滑动功能写入您的自定义类。所以不需要在滑动内再次出队

    【讨论】:

    • 一开始我在 uicollectionviewcell 自定义类中添加了手势识别器,但它正在崩溃 - 致命错误。
    • 好的。如果你可以尝试再做一次。让我知道发生什么事。到那时我也检查一次。
    • 好的。我再试一次。感谢您的帮助。
    • 您能否提供在单个集合视图单元格中有多少个 UIView?
    • 只有一个 UIView 我找到了解决方案。我将回答这个问题的解决方案
    【解决方案3】:

    在您执行图像更新后,您正在调用集合视图以再次重新加载。

    cell.updateCell()
    self.myCollectionView.reloadData()
    

    如果您没有“方法”知道单元格之前何时被刷过。 reloadData 方法将加载单元格,因为它是第一次加载而没有应用任何更改。

    更新

    但是稍后当向上/向下滚动后重新绘制单元格时,它应该会发生同样的问题。我建议将图像作为UICollectionViewCell 自定义类的属性移动。还要在该自定义类中添加一个布尔值,该类知道您的图像何时旋转。所以任何时候你必须绘制你的单元格,你只需在你的自定义类中调用你的 draw 方法。

    【讨论】:

    • 好的。我将删除该行再试一次
    • 我试过了,但当时,当我在自定义类中添加手势时,应用程序给出了致命错误。所以我想这样做
    • 我尝试按照建议删除该行,但面临同样的问题
    猜你喜欢
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多