1

我设计了一个显示平铺图案的自定义 UIImageView 类。该类旨在用于情节提要。该模式是使用 awakeFromNib 协议中的 CALayer 上的运行时参数构建的。

在我尝试使用动画转场之前,一切正常。它在模拟器上完美运行,但是当我在 iPad 上运行它时,图层是黑色的。如果我禁用动画一切正常。

有任何想法吗?

这是代码:

_hasRoundedCorners 和 _textureImage 是运行时参数

- (void) awakeFromNib {
    [super awakeFromNib];

    CALayer *textureLayer = [CALayer layer];
    [textureLayer setFrame:[self bounds]];
    if (_hasRoundedCorners) {
        [textureLayer setCornerRadius:6.0f];
    }
    UIColor *mycolor=[UIColor colorWithPatternImage:[UIImage imageNamed:_textureImage]];
    [textureLayer setBackgroundColor:mycolor.CGColor];
    [[self layer] addSublayer:textureLayer];
    }
 }

更新:我做了进一步的测试。只有一些 segue 动画会导致问题。Cover Vertical 和 Cross Dissolve 表现出这种行为。水平翻转和部分卷曲工作正常。

我还添加了其他功能(见下文)。该类现在将显示渐变或图案以及可选的投影。属性值在设计时在情节提要中设置。

@property (assign) BOOL hasDropShadow;
@property (assign) BOOL hasRoundedCorners;
@property (assign) BOOL hasGradient;
@property (strong, nonatomic) UIColor *highColor;
@property (strong, nonatomic) UIColor *lowColor;
@property (strong, nonatomic) NSString *textureImage;

- (void) awakeFromNib {
    [super awakeFromNib];
    //turning off bounds clipping allows the shadow to extend beyond the rect of the view
    if (_hasDropShadow) {
        [self setClipsToBounds:NO];
    }
    CALayer * roundRect = [CALayer layer];
    [roundRect setFrame:[self bounds]];
    if (_hasRoundedCorners) {
        [roundRect setCornerRadius:6.0f];
        [roundRect setMasksToBounds:YES];
    }
    if(_hasGradient){
        //the default colors for the gradient.  highColor is at the top, lowColor as at the bottom
        if (!_highColor){
            _highColor = [UIColor lightGrayColor];
        }
        if (!_lowColor){
            _lowColor = [UIColor darkGrayColor];
        }
        CAGradientLayer * gradient = [CAGradientLayer layer];
        [gradient setFrame:[self bounds]];
        [gradient setColors:[NSArray arrayWithObjects:(id)[_highColor CGColor], (id)[_lowColor CGColor], nil]];
        [roundRect addSublayer:gradient];
    }

    //add the rounded rect layer underneath all other layers of the view
    [[self layer] insertSublayer:roundRect atIndex:0];

    //set the shadow on the view's layer
    if (_hasDropShadow) {
        [[self layer] setShadowColor:[[UIColor blackColor] CGColor]];
        [[self layer] setShadowOffset:CGSizeMake(0, 6)];
        [[self layer] setShadowOpacity:1.0];
        [[self layer] setShadowRadius:10.0];
    }
    if(!_hasGradient){
        //set up the texture layer
        CALayer *textureLayer = [CALayer layer];
        [textureLayer setFrame:[self bounds]];
        if (_hasRoundedCorners) {
            [textureLayer setCornerRadius:6.0f];
        }
        UIColor *mycolor=[UIColor colorWithPatternImage:[UIImage imageNamed:_textureImage]];
        if (mycolor) {
            [textureLayer setBackgroundColor:mycolor.CGColor];
            [[self layer] addSublayer:textureLayer];
        }
    }
}
4

0 回答 0