Rounded UIView using CALayers - only some corners - How?

后端 未结 14 1999
南方客
南方客 2020-11-22 13:53

In my application - there are four buttons named as follows:

  • Top - left
  • Bottom - left
  • Top - right
  • Bottom - right

Abov

相关标签:
14条回答
  • 2020-11-22 14:25

    Stuarts example for rounding specific corners works great. If you want to round multiple corners like top left and right this is how to do it

    // Create the path (with only the top-left corner rounded)
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:imageview
                                                   byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight
                                                         cornerRadii:CGSizeMake(10.0, 10.0)];
    
    // Create the shape layer and set its path
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = imageview.bounds;
    maskLayer.path = maskPath.CGPath;
    
    // Set the newly created shape layer as the mask for the image view's layer
    imageview.layer.mask = maskLayer; 
    
    0 讨论(0)
  • 2020-11-22 14:31

    I used the answer over at How do I create a round cornered UILabel on the iPhone? and the code from How is a rounded rect view with transparency done on iphone? to make this code.

    Then I realized I'd answered the wrong question (gave a rounded UILabel instead of UIImage) so I used this code to change it:

    http://discussions.apple.com/thread.jspa?threadID=1683876

    Make an iPhone project with the View template. In the view controller, add this:

    - (void)viewDidLoad
    {
        CGRect rect = CGRectMake(10, 10, 200, 100);
        MyView *myView = [[MyView alloc] initWithFrame:rect];
        [self.view addSubview:myView];
        [super viewDidLoad];
    }
    

    MyView is just a UIImageView subclass:

    @interface MyView : UIImageView
    {
    }
    

    I'd never used graphics contexts before, but I managed to hobble together this code. It's missing the code for two of the corners. If you read the code, you can see how I implemented this (by deleting some of the CGContextAddArc calls, and deleting some of the radius values in the code. The code for all corners is there, so use that as a starting point and delete the parts that create corners you don't need. Note that you can make rectangles with 2 or 3 rounded corners too if you want.

    The code's not perfect, but I'm sure you can tidy it up a little bit.

    static void addRoundedRectToPath(CGContextRef context, CGRect rect, float radius, int roundedCornerPosition)
    {
    
        // all corners rounded
        //  CGContextMoveToPoint(context, rect.origin.x, rect.origin.y + radius);
        //  CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + rect.size.height - radius);
        //  CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + rect.size.height - radius, 
        //                  radius, M_PI / 4, M_PI / 2, 1);
        //  CGContextAddLineToPoint(context, rect.origin.x + rect.size.width - radius, 
        //                          rect.origin.y + rect.size.height);
        //  CGContextAddArc(context, rect.origin.x + rect.size.width - radius, 
        //                  rect.origin.y + rect.size.height - radius, radius, M_PI / 2, 0.0f, 1);
        //  CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + radius);
        //  CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + radius, 
        //                  radius, 0.0f, -M_PI / 2, 1);
        //  CGContextAddLineToPoint(context, rect.origin.x + radius, rect.origin.y);
        //  CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + radius, radius, 
        //                  -M_PI / 2, M_PI, 1);
    
        // top left
        if (roundedCornerPosition == 1) {
            CGContextMoveToPoint(context, rect.origin.x, rect.origin.y + radius);
            CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + rect.size.height - radius);
            CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + rect.size.height - radius, 
                            radius, M_PI / 4, M_PI / 2, 1);
            CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, 
                                    rect.origin.y + rect.size.height);
            CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y);
            CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y);
        }   
    
        // bottom left
        if (roundedCornerPosition == 2) {
            CGContextMoveToPoint(context, rect.origin.x, rect.origin.y);
            CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + rect.size.height);
            CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, 
                                    rect.origin.y + rect.size.height);
            CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y);
            CGContextAddLineToPoint(context, rect.origin.x + radius, rect.origin.y);
            CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + radius, radius, 
                            -M_PI / 2, M_PI, 1);
        }
    
        // add the other corners here
    
    
        CGContextClosePath(context);
        CGContextRestoreGState(context);
    }
    
    
    -(UIImage *)setImage
    {
        UIImage *img = [UIImage imageNamed:@"my_image.png"];
        int w = img.size.width;
        int h = img.size.height;
    
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
    
        CGContextBeginPath(context);
        CGRect rect = CGRectMake(0, 0, w, h);
    
    
        addRoundedRectToPath(context, rect, 50, 1);
        CGContextClosePath(context);
        CGContextClip(context);
    
        CGContextDrawImage(context, rect, img.CGImage);
    
        CGImageRef imageMasked = CGBitmapContextCreateImage(context);
        CGContextRelease(context);
        CGColorSpaceRelease(colorSpace);
        [img release];
    
        return [UIImage imageWithCGImage:imageMasked];
    }
    

    alt text http://nevan.net/skitch/skitched-20100224-092237.png

    Don't forget that you'll need to get the QuartzCore framework in there for this to work.

    0 讨论(0)
提交回复
热议问题