【问题标题】:Animation not play with UIIView subClass (DrawRect method)动画不能与 UIIView 子类一起播放(DrawRect 方法)
【发布时间】:2012-11-27 11:40:35
【问题描述】:

我有一个带有 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];

【问题讨论】:

  • 请显示一些代码。你调用super的drawRect方法了吗?
  • 是的,我称之为超级Draw rect,我的代码很简单,我会在问题中添加它。
  • 你能展示你如何初始化你的子视图并将它添加到视图控制器吗?是手动完成的吗?
  • 已添加到问题中。
  • 我在viewDidLoad中添加了。

标签: iphone objective-c animation uiview drawrect


【解决方案1】:

尝试替换

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

【讨论】:

    猜你喜欢
    • 2021-05-31
    • 2013-04-07
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    相关资源
    最近更新 更多