我有一个 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
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/