如何创建一个带有弯曲边的自定义uicollectionviewcell?

时间:2014-03-20 12:34:38

标签: ios objective-c ios7 uicollectionviewcell xcode5.1

我需要创建一个带有弯曲边的自定义UICollectionViewCell

请看下面的图片

enter image description here

此图像由两个单元格组成。

请告诉我如何制作带有弯曲边的自定义UICollectionViewCell,如上图所示 感谢

3 个答案:

答案 0 :(得分:1)

将CAShapeLayer与UIBezierPath一起使用,并在单元格视图上添加该图层。

-(UIView *)roundCornersOnView:(UIView *)view  radius:(float)radius {

    UIRectCorner corner; //holds the corner
    corner = UIRectCornerTopLeft | UIRectCornerTopRight;

    UIView *roundedView = view;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:roundedView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = roundedView.bounds;
    maskLayer.path = maskPath.CGPath;
    roundedView.layer.mask = maskLayer;
    return roundedView;

}

使用:

UIView *v1=[[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
v1.backgroundColor=[UIColor redColor];
[self.view addSubview:[self roundCornersOnView:v1 radius:50]];

答案 1 :(得分:0)

try with this one its working in my code

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
UIView *view1=[[UIView alloc]initWithFrame:cell.frame];
view1.backgroundColor=[UIColor blueColor];
UIBezierPath *maskpath=[UIBezierPath bezierPathWithRoundedRect:view1.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight|UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(10.0,10.0)];
CAShapeLayer *maskLayer=[CAShapeLayer layer];
maskLayer.frame=view1.bounds;
maskLayer.path=maskpath.CGPath;
view1.layer.mask=maskLayer;
cell.backgroundView=view1;


return cell;

}

答案 2 :(得分:0)

我找到了另一个解决方案,我没有为每个UICollectionViewCell添加形状图层,而是使用UICollectionView将形状图层添加到整个[UIBezierPath bezierPathWithArcCenter:radius:startAngle:endAngle:clockwise:]

我使用了下面提到的代码

-(UICollectionView *)roundCornersOnView:(UICollectionView *)view{



UICollectionView *roundedView = view;
UIBezierPath *maskPath ;
maskPath=[UIBezierPath bezierPathWithArcCenter:CGPointMake(160, 350) radius:350 startAngle:0 endAngle:1.5708 clockwise:NO];

CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = roundedView.bounds;
maskLayer.path = maskPath.CGPath;
roundedView.layer.mask = maskLayer;
return roundedView;
}