Ios 如何创建具有弯曲边的自定义uicollectionviewcell?
我需要创建一个带有弯曲边的自定义Ios 如何创建具有弯曲边的自定义uicollectionviewcell?,ios,objective-c,ios7,uicollectionviewcell,xcode5.1,Ios,Objective C,Ios7,Uicollectionviewcell,Xcode5.1,我需要创建一个带有弯曲边的自定义UICollectionViewCell 请看下图 此图像由两个单元格组成 请告诉我如何制作一个带有弧形边的自定义UICollectionViewCell,如上图所示。 感谢您将CAShapeLayer与UIBezierPath一起使用,并将该层添加到单元格视图中 -(UIView *)roundCornersOnView:(UIView *)view radius:(float)radius { UIRectCorner corner; //hol
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;
}
使用:
试试这个,它在我的代码中工作
-(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;
}我找到了另一种解决方案,我使用
[UIBezierPath-bezierPathWithArcCenter:radius:startAngle:endAngle:顺时针://将形状层添加到整个UICollectionView
我使用了下面提到的代码
-(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;
}
将CAShapeLayer
与UIBezierPath
一起使用,然后将该层添加到单元格视图中。@Virussmca你能告诉我有没有关于该层的教程吗?@gautam检查我的答案。你离答案很近,但代码只会使两个角变圆。也许我们应该尝试其他UIBezierPath方法。这个答案有效,但不是corner=UIRectCornerTopLeft | UIRectCornerTopRight代码>你需要做所有的角。@InfinityJames:他只想做最上面的角。@Gautham:是的,你可以试试相关的东西。
-(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;
}