Ios 尝试在手势识别时翻转单元格上的imageview,代码工作正常,但没有翻转
我有一个集合视图,在集合视图上,我拍摄了Ios 尝试在手势识别时翻转单元格上的imageview,代码工作正常,但没有翻转,ios,xcode,swift2,uicollectionview,uigesturerecognizer,Ios,Xcode,Swift2,Uicollectionview,Uigesturerecognizer,我有一个集合视图,在集合视图上,我拍摄了imageview,我在imageview上添加了uisweegestureerecognizer,工作正常。它进入swiked方法。在Swipe方法中,我试图翻转单元格上的imageview,但它不工作 下面是我澄清的代码 // collectionview delegate method in which I have added gesture. internal func collectionView(collectionView: UI
imageview
,我在imageview
上添加了uisweegestureerecognizer
,工作正常。它进入swiked方法。在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
时,会调用Update cell方法,但它不会翻转。如果我直接在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)
}
任何帮助都将不胜感激。错误在哪里?执行图像更新后,您将调用集合视图以再次重新加载
cell.updateCell()
self.myCollectionView.reloadData()
如果你没有一种“方法”来知道手机什么时候被刷过。reloadData方法将加载单元格,因为它是第一次加载,没有应用任何更改
更新
但稍后当单元格在上下滚动后重新绘制时,也会出现同样的问题。我建议将图像作为
UICollectionViewCell
自定义类的属性移动。还可以在自定义类中添加一个布尔值,该类知道图像何时旋转。因此,任何时候您必须绘制单元格,您只需在自定义类中调用draw方法。如果您使用的是uicollectionviewcell的子类,请将手势识别器指定给awakeFromNib中的图像视图。还要将滑动函数写入自定义类。因此,无需在刷卡过程中再次排队最终找到了解决方案-
//替换代码-
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
.....
}
如果有人面临同样的问题,我希望这会有所帮助
感谢以上所有试图帮助您找到解决方案的人。请启用uiimageview的用户交互,然后重试again@Spynet我已经添加了cell.myImageView.userInteractionEnabled=true;是否需要在某个地方再次添加它?一次就足够了,我会检查并更新你的意思,同时你也搜索…好的。我正在再试一次。谢谢你的帮助。我将删除该行,再试一次,但在那个时候,当我在自定义类中添加手势时,应用程序给出了致命错误。所以我想用这种方法来做,我试着按照建议删除这条线,但面对同样的问题,我在uicollectionviewcell自定义类中添加了手势识别器,但它出现了崩溃-致命错误。Ok。如果你能再试一次。让我知道会发生什么。在那之前我还要检查一次。我正在再试一次。谢谢你的帮助。你能提供一个集合视图单元格中有多少个UIView吗?我只找到了一个UIView解决方案。我将回答这个问题作为解决办法