2010-09-19 83 views
0

我正在繪製一個UIview幾個其他子視圖。然後在這個UIView的DrawRect方法中,我繪製了一條貝塞爾曲線。石英自定義繪圖和UIViews。看不到石英圖

不幸的是,貝塞爾曲線從不出現。我檢查了bezier曲線是在視圖的背景之後(當我移除背景視圖時,我可以看到它)。

如何將自定義繪圖與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 

回答

0

你正在繪製成有自己的opaque屬性設置爲YES視圖的子視圖?如果是這樣(這是默認設置)並且它們完全覆蓋了您正在繪製的視圖,那麼我不希望看到任何繪圖。

EDIT進一步建議:

如果其他意見有opaque=NO,或許還設置了背景色以清除?如果它們是在Interface Builder中創建的,則可以將其背景色設置爲「清除顏色」。如果他們是在代碼中創建的,你可以做view.backgroundColor = [UIColor clearColor]

+0

我做了你所說的(在所有的意見),但我仍然看不到圖紙。如果我將alpha值設置爲全部或部分透明度,那麼我可以看到圖紙,但當然它們與其他視圖混合在一起。我不知道還有什麼要做。我真的被困在這裏。 – Summon 2010-09-20 07:22:29