尝试在手势识别时翻转单元格上的图像视图,代码工作正常但没有翻转
Trying to flip the imageview on cell on gesture recognization, code is working fine but not getting flipped
我有一个集合视图,在集合视图中,我使用了 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)
}
如有任何帮助,我们将不胜感激。哪里错了?
执行图像更新后,您将再次调用集合视图重新加载。
cell.updateCell()
self.myCollectionView.reloadData()
如果您 'way' 不知道单元格何时被刷过。 reloadData 方法将加载单元格,因为这是第一次加载,没有应用任何更改。
更新
但是稍后当单元格在滚动后重绘时 up/down 它应该会发生同样的问题。我建议将图像移动为您的 UICollectionViewCell
自定义 class 的 属性。还要在该自定义 class 中添加一个布尔值,它知道您的图像何时旋转。所以任何时候你必须绘制你的单元格,你只需在你的自定义 class.
中调用你的绘制方法
如果您正在使用 uicollectionviewcell 的子class,则将手势识别器分配给 awakeFromNib 中的图像视图。同时将滑动功能写入您的自定义 class。因此无需在 swipe
中再次出队
终于找到了解决方案 -
//替换代码-
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
.....
}
如果有人面临同样的问题,希望对您有所帮助
感谢以上所有试图帮助找出解决方案的人。
我有一个集合视图,在集合视图中,我使用了 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)
}
如有任何帮助,我们将不胜感激。哪里错了?
执行图像更新后,您将再次调用集合视图重新加载。
cell.updateCell()
self.myCollectionView.reloadData()
如果您 'way' 不知道单元格何时被刷过。 reloadData 方法将加载单元格,因为这是第一次加载,没有应用任何更改。
更新
但是稍后当单元格在滚动后重绘时 up/down 它应该会发生同样的问题。我建议将图像移动为您的 UICollectionViewCell
自定义 class 的 属性。还要在该自定义 class 中添加一个布尔值,它知道您的图像何时旋转。所以任何时候你必须绘制你的单元格,你只需在你的自定义 class.
如果您正在使用 uicollectionviewcell 的子class,则将手势识别器分配给 awakeFromNib 中的图像视图。同时将滑动功能写入您的自定义 class。因此无需在 swipe
中再次出队终于找到了解决方案 -
//替换代码-
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
.....
}
如果有人面临同样的问题,希望对您有所帮助 感谢以上所有试图帮助找出解决方案的人。