簡體   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