Quartz 自定义绘图和 UIView。看不到石英图
我正在 UIview 上绘制其他几个子视图。然后在这个 UIView 的 DrawRect 方法中我绘制一条贝塞尔曲线。
不幸的是,贝塞尔曲线从未出现。我已经检查贝塞尔曲线是否位于视图背景后面(当我删除背景视图时我可以看到它)。
如何将自定义绘图与 Quartz 和其他 UISubview 结合起来?
视图的 UIView 保存在 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在绘制的视图的子视图的
opaque
属性是否设置为 YES?如果是这样(这是默认设置)并且它们完全覆盖了您正在绘制的视图,那么我不会期望看到您的任何绘图。编辑并进一步建议:
如果其他视图有
opaque=NO
,也许也将其背景颜色设置为透明?如果它们是在 Interface Builder 中创建的,您可以将其背景颜色设置为“清除颜色”。如果它们是在代码中创建的,您可以执行view.backgroundColor = [UIColorclearColor]
。Do the subviews of the view you're drawing into have their
opaque
property set to YES? If so (which is the default) and they entirely cover the view you are drawing into, then I would not expect to see any of your drawing.EDIT with further suggestion:
If the other views have
opaque=NO
, perhaps also set their background color to clear? If they are created in Interface Builder you can set their background color there to "Clear Color". If they are created in code you can doview.backgroundColor = [UIColor clearColor]
.