2012-11-27 39 views
2

我有一個Tap手勢識別器的視圖。當我觸摸它時,會出現一個修改背景顏色的動畫。我的問題是,當我的子類我的視圖重寫drawRect方法我的動畫不玩了... 我怎麼可以解決這個問題? 對不起,我的英語,並通過提前感謝=)動畫不能與UIIView子類一起使用(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]; 
+0

請給出一些代碼。你叫超級的drawRect方法嗎? – giorashc

+0

是的,我打電話給超級Draw rect,我的代碼非常簡單,我會在問題中加入它。 – Jonathan

+0

你可以展示如何初始化你的子視圖並將其添加到視圖控制器?手動完成嗎? – giorashc

回答

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]]; 
+0

我現在無法測試,但這是一個好主意!我會讓你知道=) – Jonathan

+0

它不工作= / – Jonathan

相關問題