Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8210699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:09:42+00:00 2026-06-07T10:09:42+00:00

When i have written code as following way.. – (void)drawRect:(CGRect)rect { CGContextRef context =

  • 0

When i have written code as following way..

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetBlendMode(context, kCGBlendModeDifference);

    sublayer = [CALayer layer];
    sublayer.backgroundColor = [UIColor redColor].CGColor;
    sublayer.opaque = NO;
    sublayer.frame = CGRectMake(30, 30, 250, 200);
    [sublayer renderInContext:context];

    sublayer1 = [CALayer layer];
    sublayer1.opaque = NO;
    sublayer1.backgroundColor = [UIColor greenColor].CGColor;
    sublayer1.frame = CGRectMake(120, 120, 100, 250);
    [sublayer1 renderInContext:context];

}

I got as following first image as

enter image description here enter image description here

The positions of sublayer, sublayer1 are not correct way because i have set frames to them. The intersection of them is blended color. Why are they not set the actual position?

But when i use addSublayer, i got as above second image. Here the positions of sublayer, sublayer1 are correct way. The intersection of them is not blended color. What happened like that.

Why i created sublayer is, when i drag sublayers the intersection part of them has to be blended color. Please anyone explain to me.

Thanks in Advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:09:44+00:00Added an answer on June 7, 2026 at 10:09 am

    This is what happens in your code: In the first case, you just drew the layers in the graphics context of the view’s underlying layer. That’s what drawRect does. The origin of the frame doesn’t affect that because it just positions sublayers inside their parent layer. But here, the layer is never added to the parent layer, so the origin does nothing at all and both shapes appear at 0, 0.

    In the second case, first the exact same thing happened as before, but because you also added the layers to the layer hierarchy, they got rendered again on top of the view’s underlying layer, at the specified position. Each sublayer is rendered in it’s own context, so the blend mode you set in drawRect has no effect on them.

    Actually you don’t need sublayers at all if you just want to get an image of the two rectangles. You can draw them directly in drawRect in the view’s underlying layer:

    - (void)drawRect:(CGRect)rect
    {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextSetBlendMode(ctx, kCGBlendModeDifference);
    
        CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
        CGRect rect1 = CGRectMake(30, 30, 250, 200);
        CGContextFillRect(ctx, rect1);
    
        CGContextSetFillColorWithColor(ctx, [UIColor greenColor].CGColor);
        CGRect rect2 = CGRectMake(120, 120, 100, 250);
        CGContextFillRect(ctx, rect2);
    }
    

    Edit (because the question was edited):

    You can use this approach for dragging layers like this:

    • Draw a rectangle for each moving layer on a new layer or view on top of them, with the blend mode and drawing commands described above
    • Use the current frames of the moving layers for the drawing rects
    • Make sure that the new top layer doesn’t receive touches
    • Redraw the new layer whenever the position of an interactive layer is changed
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written my code in following way in cocos2d. id actionTo = [CCFadeOut
i have written the following code class Program { static void Main(string[] args) {
I have written following code to attach gesture recogniser to multiple imageviews. [imageview1 setUserInteractionEnabled:YES];
I have written a code and i'm pretty much stuck. In the following code,
I am trying my hands on WPF MVVM. I have written following code in
I have written the following code, CrystalDecisions.CrystalReports.Engine.ReportDocument report = new CrystalDecisions.CrystalReports.Engine.ReportDocument(); report.Load(@C:\Users\XXX\Desktop\Backup1\Project\ReportsFolder\ReportSalesInvoice.rpt); Report works
I have written a following code to get just the file name without extension
I have written the following code choice /m Do you want to add another
I have written the following code. But it is removing only &nbsp; not <br>
i have written the following code: As you can see there is a for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.