0

我正在 UIview 上绘制其他几个子视图。然后在这个 UIView 的 DrawRect 方法中,我绘制了一条贝塞尔曲线。

不幸的是,贝塞尔曲线从未出现过。我检查了贝塞尔曲线是否在视图的背景后面(当我删除背景视图时,我可以看到它)。

如何将自定义绘图与 Quartz 和其他 UISubviews 结合起来?

视图的 UIViews 保存在 xib 中。我加载 xib,然后在视图的 Drawrect 方法中绘制曲线。

这是我说的UIView的实现代码:

#import "DandiGameView.h"
#import "SeedView.h"
#import "Defines.h"

@implementation DandiGameView

@synthesize flowerHeadPosition, blowVolume, seedArray, trunkTop;

- (void)awakeFromNib {
 [super awakeFromNib];
 //self.clearsContextBeforeDrawing = YES;
 [self animateTrunk:1 andWindVolume:1.0];
 self.seedArray = [NSMutableArray array];
 UIImageView *tmpImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"TRUNKTOP.PNG"]];
 [tmpImageView setCenter:flowerHeadPosition];
 self.trunkTop = tmpImageView;
 [tmpImageView release];
 //[tmpImageView sett]
 [self addSubview:self.trunkTop];
 [tmpImageView release];
}

- (id)initWithCoder:(NSCoder *)decoder
{
    if (self = [super initWithCoder:decoder])
    {
  // Initialization code
  float pointX =  FLOWER_POSITION_HOR - FLOWER_TRUNK_WIDTH / 2.0;
  float topPointY =  self.bounds.size.height - FLOWER_POSITION_VER;
  flowerHeadPosition = CGPointMake(pointX, topPointY);
  s = CGPointMake(pointX, self.bounds.size.height);
  e = flowerHeadPosition;
  cp1 = CGPointMake(pointX + 10.0, self.bounds.size.height - FLOWER_LOWER_CONTROLPOINT);
  cp2 = CGPointMake(pointX - 10.0, self.bounds.size.height - FLOWER_UPPER_CONTROLPOINT);
    }
    return self;
}

- (void) animateTrunk:(double)time andWindVolume: (double) windVolume {
 randOne = sin(0.2 * time * windVolume) + sin(0.5 * time* windVolume);
 randTwo = sin(0.35 * time* windVolume) - sin(0.15 * time * windVolume);
 flowerHeadPosition.x = FLOWER_POSITION_HOR - FLOWER_TRUNK_WIDTH / 2.0 - randTwo * randOne * FLOWER_CURVITY_CONTROL;
 cp1.x = FLOWER_POSITION_HOR - FLOWER_TRUNK_WIDTH / 2.0 + 10 + randTwo * randOne * 2.0;
 [self.trunkTop setCenter:flowerHeadPosition];
 [self bringSubviewToFront:trunkTop];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {
    // Drawing code
 CGContextRef c = UIGraphicsGetCurrentContext();

    // Drawing with a GREEN stroke color
 CGContextSetRGBStrokeColor(c, 141.0/255.0, 198.0/255.0, 63.0/255.0, 1.0);
 // Draw them with a FLOWER_TRUNK_WIDTH stroke width so they are a bit more visible.
 CGContextSetLineWidth(c, FLOWER_TRUNK_WIDTH);
 // Draw a bezier curve with end points s,e and control points cp1,cp2
 e = flowerHeadPosition;
 CGContextMoveToPoint(c, s.x, s.y);
 CGContextAddCurveToPoint(c, cp1.x, cp1.y, cp2.x, cp2.y, e.x, e.y);
 CGContextStrokePath(c);
}

-(void)onElementTouch:(id)sender {

}

- (void) onTimeTick:(id)sender {
 static float runningTime = 0.0;
 runningTime += ANIMATION_TIME_STEP;
 [self animateTrunk:runningTime andWindVolume:pow(blowVolume, 5.0)];
}


- (void)dealloc {
 [trunkTop release];
 [delegate release];
    [super dealloc];
}


@end
4

1 回答 1

0

您正在绘制的视图的子视图是否将其opaque属性设置为 YES?如果是这样(这是默认设置)并且它们完全覆盖了您正在绘制的视图,那么我不希望看到您的任何绘图。

编辑进一步建议:

如果其他视图有opaque=NO,也许也将它们的背景颜色设置为清除?如果它们是在 Interface Builder 中创建的,您可以将它们的背景颜色设置为“清除颜色”。如果它们是在代码中创建的,您可以这样做view.backgroundColor = [UIColor clearColor]

于 2010-09-19T20:55:48.127 回答