I have a UIImageView on UIView.
On that I have drawn a shape using UIBezierPath. Now I want other part to be transparent.
My code is,
- (void)drawRect:(CGRect)rect
{
self.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
imgV.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
aPath = [UIBezierPath bezierPath];
CGContextRef aRef ;
aRef = UIGraphicsGetCurrentContext();
[[UIColor clearColor] setStroke];
[[UIColor clearColor] setFill];
aPath.lineWidth = 2;
aPath = [UIBezierPath new];
//path is calculated here using addLineToPoint addArcWithCentre methods
[aPath fill];
[aPath stroke];
[self setClippingPath:aPath :imgV];
[[[UIColor clearColor] colorWithAlphaComponent:0.0] setFill];
CGContextFillRect(aRef, rect);
CGContextFillPath(aRef);
self.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
imgV.backgroundColor = [[UIColor clearColor] colorWithAlphaComponent:0.0];
}
- (void) setClippingPath:(UIBezierPath *)clippingPath : (UIImageView *)imgView;
{
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = imgView.frame;
maskLayer.path = [clippingPath CGPath];
maskLayer.backgroundColor = [[[UIColor clearColor] colorWithAlphaComponent:0.0] CGColor];
maskLayer.fillColor = [[UIColor whiteColor] CGColor];
[imgView removeFromSuperview];
imgView.layer.mask = maskLayer;
[self addSubview:imgView];
}
Output can be seen in following image. Black part around that image cur should be transparent
Please help me.
I think you might be missing one last call, probably right before addSubview:
imgView.clipsToBounds = YES;
Related
I have the following code in my UITableViewCell:
[self.layer setBorderColor:[UIColor blackColor].CGColor];
[self.layer setShadowRadius:10.0];
[self.layer setCornerRadius:5.0];
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(5.0, 5.0)];
[self.layer setShadowPath:path.CGPath];
[self.layer setShouldRasterize:YES];
[self.layer setRasterizationScale:[UIScreen mainScreen].scale];
when I run instrument and set color offscreen - rendered yellow, this causes the cell to be yellow. When I remove the shouldRasterize it doesn't shade the cells to yellow. What are ways to improve this?
This is greatly hurting my scrolling performance. I am just trying to set rounded corners with some shadows in it.
I'm doing rounded corners like this:
self.layer.shadowColor = [UIColor grayColor].CGColor;
self.layer.shadowOffset = CGSizeMake(0.05, 0.05);
self.layer.shadowOpacity = 10;
self.layer.shadowRadius = 1.5;
self.layer.masksToBounds = NO;
self.layer.shouldRasterize = YES;
[self.layer setBorderColor: [[UIColor whiteColor] CGColor]];
[self.layer setBorderWidth: 5.0];
I was wondering if anyone was ever successful in changing the corners of an UIPageViewController (the fancy book turning animation of iBooks) to rounded corners?
I tried this but to no avail:
[self.notebookPages setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:NULL];
self.notebookPages.dataSource = self.pageModelController;
self.notebookPages.doubleSided = NO;
[self addChildViewController:self.notebookPages];
// mask
CAShapeLayer *maskLayer = [CAShapeLayer layer];
UIBezierPath *roundedPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 332, 480)
byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight
cornerRadii:CGSizeMake(15.f, 15.f)];
maskLayer.fillColor = [[UIColor whiteColor] CGColor];
maskLayer.backgroundColor = [[UIColor clearColor] CGColor];
maskLayer.path = [roundedPath CGPath];
self.notebookPages.view.layer.mask = maskLayer;
[self.notebookScrollNavigationController.notebook.pages addSubview:self.notebookPages.view];
self.notebookPages.view.frame = CGRectMake(0, 0, 332, 480);
[self.notebookPages didMoveToParentViewController:self];
This is all a bit hardcoded (which is obviously bad) but I was just trying to find out if rounded corners would work. However, I only get a non-rounded transparent corner:
Add this import:
#import <QuartzCore/QuartzCore.h>
Now you can use cornerRadius:
self.notebookPages.view.layer.cornerRadius = 6;
How to make a half rounded (Top corner rounded) textview or tableview with the borderwidth and borderColor?
This isn't perfect, but you can work from this:
#import <QuartzCore/CoreAnimation.h>
(and also link to QuartzCore.framework in your project), then..
self.textView.layer.borderColor = [UIColor redColor].CGColor;
self.textView.layer.borderWidth = 4.0f;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.textView.bounds
byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
cornerRadii:CGSizeMake(7.0, 7.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.textView.bounds;
maskLayer.path = maskPath.CGPath;
self.textView.layer.mask = maskLayer;
[maskLayer release];
First remove all code that would generate a border (xib or layer.borderWidth) and them use this method below that I created in a UIView Category:
#import "UIView+RoundedCorners.h"
#implementation UIView (RoundedCorners)
#pragma mark - Public
- (void)addBorderWithRoundedCorners:(UIRectCorner)roundedCorners radius:(CGFloat)radius color:(UIColor *)color
{
self.clipsToBounds = NO;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:roundedCorners cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
maskLayer.lineWidth = 1.0;
maskLayer.strokeColor = color.CGColor;
maskLayer.fillColor = [UIColor clearColor].CGColor;
[self.layer addSublayer:maskLayer];
}
#end
i think use bellow code
[yourTextView.layer setBorderColor: [[UIColor redColor] CGColor]];
[yourTextView.layer setBorderWidth: 1.0];
[yourTextView.layer setCornerRadius:8.0f];
[yourTextView.layer setMasksToBounds:YES];
also use yourTableView insted of yourTextView
I have take a lot of patient and make the rectangle in glow effect as per my requirement.
CALayer *bevelLayer = [CALayer layer];
[bevelLayer setBounds:CGRectMake(0.0f, 0.0f, 300.0f, 300.0f)];
[bevelLayer setPosition:CGPointMake(300.0f, 550.0f)];
[bevelLayer setBackgroundColor:[[UIColor whiteColor] CGColor]];
[bevelLayer setShadowOpacity:1.0];
[bevelLayer setShadowRadius:7.0f];
[bevelLayer setShadowColor:[[UIColor colorWithRed:0.0f/255.0 green:126.0f/255.0f blue:255.0f/255.0f alpha:1.0f] CGColor]];
[bevelLayer setShadowPath:[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(-10.0f, -10.0f, 310.0f, 310.0f) cornerRadius:5.0f] CGPath]];
[[[self view] layer] addSublayer:bevelLayer];
Instead of adding a new layer, why dont you directly try it on self.view..
Also, shadowOpacity is a value between 0 and 1... So giving 10 is definitely not going to help you.
If you want to try in self.view, here is the code:
self.view.layer.shadowColor = [[UIColor greenColor] CGColor];
self.view.layer.shadowOffset = CGSizeMake(1, 1);
self.view.layer.shadowOpacity = 1;
self.view.layer.shadowRadius = 20;
This way you dont have to worry about the bounds of the layer..
I want to write some words to one layer. but when I create the layer,I can't find the corresponding API to attach the words to the layer.
layer2 = [CALayer layer];
[layer2 setBackgroundColor:[[UIColor grayColor] CGColor]];
layer2.bounds = CGRectMake(0, 0, 60,40);
layer2.position = CGPointMake(25,25);//
layer2.contentsRect = layer2.bounds;
layer2.contents=#"Hello World~~"; //It's nothing in the showing layer .
[self.layer addSublayer:layer2];
contents property works only with CGImageRef. If you wish to use text on it, use CATextLayer.
Example Usage
CATextLayer * textLayer = [CATextLayer layer];
textLayer.backgroundColor = [[UIColor darkGrayColor] CGColor];
textLayer.foregroundColor = [[UIColor whiteColor] CGColor];
textLayer.bounds = CGRectMake(0, 0, 60, 40);
textLayer.position = CGPointMake(25, 25);
textLayer.string = #"Hello World";
textLayer.font = CGFontCreateWithFontName(CFSTR("Helvetica"));
textLayer.fontSize = 15.0;
[self.window.layer addSublayer:textLayer];
Of course, I am leaking memory with CGFontCreateWithFontName. You can fix that by assigning it to a variable and releasing it later.