动画不使用UIIView子类(DrawRect方法)播放



我有一个带有点击手势识别器的视图。当我触摸它时,有一个修改背景颜色的动画。我的问题是,当我对视图进行子类化以覆盖 drawRect 方法时,我的动画不再播放......我该如何解决这个问题?对不起我的英语,提前感谢=)

- (void)drawRect:(CGRect)rect
  {
    [super drawRect:rect];
    NSLog(@"DrawRect test");
    //H Black line
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGFloat black[4] = {0.0f, 0.0f, 0.0f, 0.5f};
    CGContextSetStrokeColor(c, black);
    CGContextBeginPath(c);
    CGContextMoveToPoint(c, 319.0f, 0.0f);
    CGContextAddLineToPoint(c, 319.0f, 55.0f);
    CGContextStrokePath(c);
    //H White Line
    c = UIGraphicsGetCurrentContext();
    CGFloat white[4] = {1.0f, 1.0f, 1.0f, 0.1f};
    CGContextSetStrokeColor(c, white);
    CGContextBeginPath(c);
    CGContextMoveToPoint(c, 0.0f, 1.0f);
    CGContextAddLineToPoint(c, 320.0f, 1.0f);
    CGContextStrokePath(c);
    //V White Line
    c = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColor(c, black);
    CGContextBeginPath(c);
    CGContextMoveToPoint(c, 0.0f, 54.0f);
    CGContextAddLineToPoint(c, 320.0f, 54.0f);
    CGContextStrokePath(c);
}

还有我的动画:

 //Debut
if (sender.state == UIGestureRecognizerStateBegan) {
    [UIView animateWithDuration:0.7 animations:^{
        [self.foreView setBackgroundColor:RGBA(RED_CELL, GREEN_CELL, BLUE_CELL, 0.6f)];
    }];
}
//Fin
if (sender.state == UIGestureRecognizerStateEnded) {
    [UIView animateWithDuration:0.4 animations:^{
        [self.foreView setBackgroundColor:[UIColor clearColor]];
    }];
}

如何添加我的子视图

    //foreView
    _foreView  = [[ForeViewCell alloc] initWithFrame:CGRectMake(0, 0, 320, 55)];
    [self.foreView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.foreView setBackgroundColor:[UIColor clearColor]];
    [self.contentView addSubview:self.foreView];

尝试替换

[self.foreView setBackgroundColor:RGBA(RED_CELL, GREEN_CELL, BLUE_CELL, 0.6f)];
[self.foreView setBackgroundColor:[UIColor clearColor]];

[self.foreView.layer setBackgroundColor:[RGBA(RED_CELL, GREEN_CELL, BLUE_CELL, 0.6f) CGColor]];
[self.foreView.layer setBackgroundColor:[[UIColor clearColor] CGColor]];

最新更新