【发布时间】: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