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 8035089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:12:44+00:00 2026-06-05T02:12:44+00:00

I’m trying to create a UIView which shows a semitransparent circle with an opaque

  • 0

I’m trying to create a UIView which shows a semitransparent circle with an opaque border inside its bounds. I want to be able to change the bounds in two ways – inside a -[UIView animateWithDuration:animations:] block and in a pinch gesture recogniser action which fires several times a second. I’ve tried three approaches based on answers elsewhere on SO, and none are suitable.

  1. Setting the corner radius of the view’s layer in layoutSubviews gives smooth translations, but the view doesn’t stay circular during animations; it seems that cornerRadius isn’t animatable.

  2. Drawing the circle in drawRect: gives a consistently circular view, but if the circle gets too big then resizing in the pinch gesture gets choppy because the device is spending too much time redrawing the circle.

  3. Adding a CAShapeLayer and setting its path property in layoutSublayersOfLayer, which doesn’t animate inside UIView animations since path isn’t implicitly animatable.

Is there a way for me to create a view which is consistently circular and smoothly resizable? Is there some other type of layer I could use to take advantage of the hardware acceleration?

UPDATE

A commenter has asked me to expand on what I mean when I say that I want to change the bounds inside a -[UIView animateWithDuration:animations:] block. In my code, I have a view which contains my circle view. The circle view (the version that uses cornerRadius) overrides -[setBounds:] in order to set the corner radius:

-(void)setBounds:(CGRect)bounds
{
    self.layer.cornerRadius = fminf(bounds.size.width, bounds.size.height) / 2.0;
    [super setBounds:bounds];
}

The bounds of the circle view are set in -[layoutSubviews]:

-(void)layoutSubviews
{
    // some other layout is performed and circleRadius and circleCenter are
    // calculated based on the properties and current size of the view.

    self.circleView.bounds = CGRectMake(0, 0, circleRadius*2, circleRadius*2);
    self.circleView.center = circleCenter;
}

The view is sometimes resized in animations, like so:

[UIView animateWithDuration:0.33 animations:^(void) {
    myView.frame = CGRectMake(x, y, w, h);
    [myView setNeedsLayout];
    [myView layoutIfNeeded];
}];

but during these animations, if I draw the circle view using a layer with a cornerRadius, it goes funny shapes. I can’t pass the animation duration in to layoutSubviews so I need to add the right animation within -[setBounds].

  • 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-05T02:12:46+00:00Added an answer on June 5, 2026 at 2:12 am

    With many thanks to David, this is the solution I found. In the end what turned out to be the key to it was using the view’s -[actionForLayer:forKey:] method, since that’s used inside UIView blocks instead of whatever the layer’s -[actionForKey] returns.

    @implementation SGBRoundView
    
    -(CGFloat)radiusForBounds:(CGRect)bounds
    {
        return fminf(bounds.size.width, bounds.size.height) / 2;
    }
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [UIColor clearColor];
            self.opaque = NO;
            self.layer.backgroundColor = [[UIColor purpleColor] CGColor];
            self.layer.borderColor = [[UIColor greenColor] CGColor];
            self.layer.borderWidth = 3;
            self.layer.cornerRadius = [self radiusForBounds:self.bounds];
        }
        return self;
    }
    
    -(void)setBounds:(CGRect)bounds
    {
        self.layer.cornerRadius = [self radiusForBounds:bounds];
        [super setBounds:bounds];
    }
    
    -(id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event
    {
        id<CAAction> action = [super actionForLayer:layer forKey:event];
    
        if ([event isEqualToString:@"cornerRadius"])
        {
            CABasicAnimation *boundsAction = (CABasicAnimation *)[self actionForLayer:layer forKey:@"bounds"];
                if ([boundsAction isKindOfClass:[CABasicAnimation class]] && [boundsAction.fromValue isKindOfClass:[NSValue class]])
            {            
                CABasicAnimation *cornerRadiusAction = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
                cornerRadiusAction.delegate = boundsAction.delegate;
                cornerRadiusAction.duration = boundsAction.duration;
                cornerRadiusAction.fillMode = boundsAction.fillMode;
                cornerRadiusAction.timingFunction = boundsAction.timingFunction;
    
                CGRect fromBounds = [(NSValue *)boundsAction.fromValue CGRectValue];
                CGFloat fromRadius = [self radiusForBounds:fromBounds];
                cornerRadiusAction.fromValue = [NSNumber numberWithFloat:fromRadius];
    
                return cornerRadiusAction;
            }
        }
    
        return action;
    }
    
    @end
    

    By using the action that the view provides for the bounds, I was able to get the right duration, fill mode and timing function, and most importantly delegate – without that, the completion block of UIView animations didn’t run.

    The radius animation follows that of the bounds in almost all circumstances – there are a few edge cases that I’m trying to iron out, but it’s basically there. It’s also worth mentioning that the pinch gestures are still sometimes jerky – I guess even the accelerated drawing is still costly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.