繁体   English   中英

如何创建具有弯曲侧面的自定义uicollectionviewcell?

[英]How to create a custom uicollectionviewcell with a curved side?

我需要创建一个具有弯曲侧面的自定义UICollectionViewCell

请看下面的图片

在此处输入图片说明

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

请告诉我如何制作具有弯曲侧面的自定义UICollectionViewCell ,如上图所示。
谢谢

为此,将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]];

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;

}

我已经发现一个用于它的另一解决方案,而不是添加形状层到每个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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM