动画不能与UIIView子类一起使用(DrawRect方法)

时间:2012-11-27 11:40:35

标签: iphone objective-c animation uiview drawrect

我有一个Tap手势识别器的视图。当我触摸它时,会有一个修改背景颜色的动画。我的问题是,当我继承我的视图以覆盖drawRect方法时,我的动画不再播放了... 我怎么能解决这个问题? 对不起我的英语,感谢advance =)

- (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];

1 个答案:

答案 0 :(得分: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]];