我正在 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