Hello everyone,
I am drawing on a UIview several other subviews. Then in the DrawRect method of this UIView I am drawing a bezier curve.
UNFORTUNATELY, the bezier curve never shows up. I have checked that the bezier curve is behind the background of the view (when I remove the background view I can see it).
How can I combine custom drawing with Quartz and other UISubviews?
The UIViews of the view are kept in a xib. I load the xib and then I draw the curve in the Drawrect method of the view.
This is the implementation code of the UIView I am talking about:
#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