将CAShapeLayer应用于Cells内的多个UIImageViews

时间:2014-05-09 12:15:36

标签: ios objective-c cashapelayer

我试图围绕UIImageViewUICollectionViewCell的顶角。它与我的第一个代码一起工作但是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的建议,尝试将creata作为UIImageView的子类RoundedImageView,现在代码如下:

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类设置为RoundedImagView,并在我的自定义UICollectionViewCell中设置:

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

但是我没有调用RoundedImageView.m中的viewDidLoad或drawRect。

3 个答案:

答案 0 :(得分:1)

为此您必须创建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

我认为上面的方法会起作用..只是尝试测试它。

答案 1 :(得分:0)

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

cell.image.layer.mask = maskLayer;

此处在分配masklayer时不会创建新副本。

编辑:

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

在cellForRowAtIndexPath中:执行以下操作

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

答案 2 :(得分:0)

您不需要覆盖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;
}