Re: Relation of UIView layer to drawRect:
Re: Relation of UIView layer to drawRect:
- Subject: Re: Relation of UIView layer to drawRect:
- From: "David F." <email@hidden>
- Date: Wed, 25 Aug 2010 14:10:07 -0600
Got it. Thanks.
David
On Aug 24, 2010, at 11:41 PM, Luke Hiesterman wrote:
> Adding a sublayer to self.layer works just like adding a subview to self. Sublayers are drawn on top of their superlayers. In this case the superlayer is your view, so the image in the sublayer will be rendered on top of whatever you draw in drawRect:
>
> Luke
>
> Sent from my iPhone.
>
> On Aug 24, 2010, at 8:47 PM, David F. <email@hidden> wrote:
>
>> What is the relationship of a UIView's layer (and its sublayers) to what happens when that UIView's drawRect: is called? I would expect the code below to draw a diagonal line across (i.e. on top of) the image, but it looks like the line is drawn behind the image. In other words, all I see is the image.
>>
>> @implementation MyView
>>
>> - (void)awakeFromNib {
>> CALayer *imgLayer = [CALayer layer];
>> imgLayer.position = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
>> imgLayer.bounds = self.bounds;
>> imgLayer.contents = (id)[[UIImage imageNamed: @"myimage.jpg"] CGImage];
>> [self.layer addSublayer: imgLayer];
>> }
>>
>> - (void)drawRect: (CGRect)rect {
>> CGMutablePathRef path = CGPathCreateMutable();
>> CGPathMoveToPoint(path, NULL, 0, 0);
>> CGPathAddLineToPoint(path, NULL, self.bounds.size.width, self.bounds.size.height);
>>
>> CGContextRef context = UIGraphicsGetCurrentContext();
>> UIColor *black = [UIColor colorWithRed: 0 green: 0 blue: 0 alpha: 0.5];
>> CGContextSetStrokeColorWithColor(context, black.CGColor);
>> CGContextSetLineWidth(context, 10);
>> CGContextSetLineCap(context, kCGLineCapRound);
>> CGContextAddPath(context, path);
>> CGContextStrokePath(context);
>> CFRelease(path);
>> }
>>
>> @end
>>
>> Any hints?
>>
>> Thanks,
>> David
>>
>> _______________________________________________
>>
>> Cocoa-dev mailing list (email@hidden)
>>
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden