Ios 将CAShapeLayer应用于单元格内的多个UIImageView

Ios 将CAShapeLayer应用于单元格内的多个UIImageView,ios,objective-c,cashapelayer,Ios,Objective C,Cashapelayer,我正试图在UICollectionViewCell内绕过UIImageView的上角。它与我的第一个代码一起工作,但是UICollectionViews的性能下降了很多,开始落后。为了防止出现这种情况,我尝试创建一个变量,只为第一个单元格生成一个CAShapeLayer,然后对其余单元格应用相同的掩码。这是我的密码: 在cellForItemAtIndexPath中 In if(maskLayer == nil){ UIBezierPath *maskPath;

我正试图在
UICollectionViewCell
内绕过
UIImageView
的上角。它与我的第一个代码一起工作,但是UICollectionViews的性能下降了很多,开始落后。为了防止出现这种情况,我尝试创建一个变量,只为第一个单元格生成一个CAShapeLayer,然后对其余单元格应用相同的掩码。这是我的密码:

cellForItemAtIndexPath中

In if(maskLayer == nil){
        UIBezierPath *maskPath;
        maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.image.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(3.0, 3.0)];
        maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = cell.image.bounds;
        maskLayer.path = maskPath.CGPath;
    }

    cell.image.layer.mask = maskLayer;

...
远高于:

@implementation MainViewViewController{    
    CAShapeLayer *maskLayer;

}
但这是行不通的。桅杆仅应用于滚动时进入视图的最后一个单元格,并从其余单元格中移除。这个解决方案是否可行?为什么不可行

提前谢谢

编辑1 按照@debugger的建议,尝试创建名为
RoundedImageView
UIImageView
的子类,现在代码如下所示:

RoundedImageView.m:

#import "RoundedImageView.h"

@implementation RoundedImageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)viewDidLoad{
    UIBezierPath *maskPath;
    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(3.0, 3.0)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    UIBezierPath *maskPath;
    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(3.0, 3.0)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}
在我的故事板中,我已将UIImageView类设置为RoundEdImageView,并在自定义UICollectionViewCell中:

@property (strong, nonatomic) IBOutlet RoundedImageView *image;

但是我发现RoundedImageView.m中的viewDidLoad或drawRect根本没有被调用

为此,必须创建UIImageview扩展类名RoundImageview。并将ViewDidLoad方法中的以下代码添加到类中:

self.contentMode = UIViewContentModeScaleAspectFill; 
self.clipsToBounds = YES;
self.layer.cornerRadius = self.frame.size.width/2; // Setting the corner radius
self.layer.masksToBounds = YES;
现在,在viewcontroller类中,在标题中添加UIImageview扩展并初始化,如下所示:

RoundImageview *roundimageview;
在cellForRowAtIndexPath方法中,使用以下方法添加图像:

rounfimageview.image = yourimagename

我认为上面的方法会起作用。试着测试一下。

为cell创建CAShapeLayer。当您创建单个实例并尝试为所有单元格添加时,它将添加到最后一个实例

cell.image.layer.mask = maskLayer;
在这里,当您指定masklayer时,它不会创建新副本

编辑:

层的单个实例如何成为多个层的子层

在CellForRowatineXpath中:执行以下操作

if (!cell) {
// create cell
//And create mask layer
cell.image.layer.mask = maskLayer; // - And do this
}

您不需要重写drawRect:,也不希望将代码放入viewDidLoad,因为这是UIViewController方法,不会在UIImageView子类中调用。由于图像视图是在故事板中创建的,因此您应该将代码放在UIImageView子类的initWithCoder:方法中

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(3.0, 3.0)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
    }
    return self;
}

嗨,谢谢。但是,当我创建UIImageView的子类时,我无法调用该类。我在viewDidLoad和DrawRect中有断点您是否提到了新创建的类和视图控制器,并在视图控制器中添加了引用?我将情节提要中的UIImageView类更改为RoundeImageView,并更改了自定义UICollectionViewCell中的属性我已经在使用此方法。可能您在某些方面有错误指向没有代码,我无法解释你错在哪里。对不起,这与我正在做的有什么不同?@PaperThick你尝试过为每个单元格创建shapelayer吗?是的,我尝试过。然后我得到圆角,但滚动时CollectionView滞后如果
RoundedImageView
UIImageView
的子类,则不会调用
drawRect:
。您需要将
UIView
子类化,将
clipstobunds
设置为
YES
,并将
UIImageView
添加为子视图。