我有一个简单的问题,我无法在谷歌,文档或这里找到解决方案.
我的视图控制器中有一个Collectionview.我创建了一个包含UIImage的自定义单元格DescriptionCell.我希望这个图像有圆角.但是,我不知道在UIImage层上设置cornerradius的位置.我已尝试在单元格的awakeFromNib方法中,在委托方法CellForRowAtIndexPath中覆盖单元格中的LayoutSubview但它不起作用.我应该在哪里放置代码来设置UIImage的半径?
要指定,我知道如何创建UIImage的圆角.但如果它是Collectionview单元格的子视图,我不知道在哪里设置cornerradius.
这是我的descriptionCell的代码
class DescriptionCell: UICollectionViewCell {
@IBOutlet weak var mImage: UIImageView!
override func awakeFromNib() {
//mImage.layer.cornerradius = 5
//Does not work, the image is still square in the cell
}
Run Code Online (Sandbox Code Playgroud)
并在cellforRowAtIndePath中
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("descriptioncell", forIndexPath: indexPath) as! DescriptionCell
//cell.mImage.layer.cornerradius = 5
//Does not work, the image is still square in the cell
return cell
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!
在过去的几个小时里,我试图完成以下任务:
在UICollectionview中,当我突出显示(向下触摸)一个单元格时,我希望它向内反射几个像素(更小的尺寸)并在释放时弹回到原始大小.
这是我到目前为止所尝试的:
override func collectionView(collectionView: UICollectionView,
didHighlightItemAtIndexPath indexPath: NSIndexPath) {
collectionView.collectionViewLayout.invalidateLayout()
let cell = collectionView.cellForItemAtIndexPath(indexPath)
UIView.transitionWithView(cell!, duration: 1, options: UIViewAnimationOptions.CurveEaseIn, animations: {
let center = cell?.center
let frame2 = CGRect(x: 0, y: 0, width: 50, height: 50)
cell?.frame = frame2
cell?.center = center!
}, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
(我从这个问题中找到了很多代码示例: UICollectionView:选择时动画单元格大小会发生变化,但这适用于单元格选择.不突出显示.)
所以在didHighlightItemAtIndexPath中我将突出显示的单元格设置为新的大小.大小设置正确但出现以下问题:1:它没有动画,2:它移动到位置(0,0)并动画回到中心位置.它根本不应该移动位置 - 只是尺寸.
我在Shazam应用程序中看到了这个效果以供参考.当您在歌曲名称下方的按钮上对一首歌曲进行shazam(或查看以前的shazamed歌曲)时,可以看到它.