2

我有一个带有 Tap 手势识别器的视图。当我触摸它时,会出现一个修改背景颜色的动画。我的问题是,当我将我的视图子类化以覆盖 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];
4

1 回答 1

0

尝试更换

[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]];
于 2012-11-27T13:28:18.070 回答