im learning CoreGraphic and want to make a simple game, but stuck on drawing thing with bezier path, i wanted to draw a triangle inside a subview, but it always come out wrong, i want it to fit 1/4 of the square view
My code:
UIBezierPath* trianglePath = [UIBezierPath bezierPath];
[trianglePath moveToPoint:CGPointMake(0, 0)];
[trianglePath addLineToPoint:CGPointMake(self.mainView.frame.size.width/2, self.mainView.frame.size.height/2)];
[trianglePath addLineToPoint:CGPointMake(self.mainView.frame.size.width, 0)];
[trianglePath closePath];
CAShapeLayer *triangleMaskLayer = [CAShapeLayer layer];
[triangleMaskLayer setPath:trianglePath.CGPath];
UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.mainView.frame.size.width, self.mainView.frame.size.height)];
firstView.backgroundColor = [UIColor colorWithWhite:.75 alpha:1];
firstView.layer.mask = triangleMaskLayer;
[self.mainView addSubview:firstView];
It look like this:
If the dimensions are incorrect, you probably are creating the triangle before AutoLayout has done its job.
To ensure your self.mainView has the correct size, create your triangle in your controller viewDidLayoutSubviews method.
Also mind that viewDidLayoutSubviews may be called multiple times.
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void) viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (self.mainView.subviews.count == 0) {
UIBezierPath* trianglePath = [UIBezierPath bezierPath];
[trianglePath moveToPoint:CGPointMake(0, 0)];
[trianglePath addLineToPoint:CGPointMake(self.mainView.frame.size.width/2, self.mainView.frame.size.height/2)];
[trianglePath addLineToPoint:CGPointMake(self.mainView.frame.size.width, 0)];
[trianglePath closePath];
CAShapeLayer *triangleMaskLayer = [CAShapeLayer layer];
[triangleMaskLayer setPath:trianglePath.CGPath];
UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.mainView.frame.size.width, self.mainView.frame.size.height)];
firstView.backgroundColor = [UIColor colorWithWhite:.75 alpha:1];
firstView.layer.mask = triangleMaskLayer;
[self.mainView addSubview:firstView];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Related
I'm making mask using UIBezierPath and CAShapeLayer with this code:
- (void)createPath {
UIBezierPath *path = [[UIBezierPath alloc] init];
[path moveToPoint:CGPointMake(0, 0)];
[path addLineToPoint:CGPointMake(100, 100)];
[path moveToPoint:CGPointMake(100, 100)];
[path addLineToPoint:CGPointMake(0, 100)];
[path moveToPoint:CGPointMake(0, 100)];
[path addLineToPoint:CGPointMake(0, 0)];
[path closePath];
CAShapeLayer *layer = [CAShapeLayer new];
layer.frame = self.contentView.bounds;
layer.path = path.CGPath;
self.contentView.layer.mask = layer;
}
But instead of masking my contentView disappears completely. I tried to look at path in debugger and it looks exactly as I want it to look.
When using the layer.mask, the first is to get the right path. You don't need to move to a new point every time. By that way, your path is made of three or four subpaths which can not be closed to form a right path.
The second is trying to use in the View class itself, not to call for other subviews, like contentView. Because you may not know when to call it in subviews. Run the following in a UIView subclass, like in a UITableViewCell (awake from nib). You can get what I mean. If you really want to use contentView, just find the right position to put your layer code. like override setNeedLayout, etc.
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
[self createPath];
}
- (void)createPath {
UIBezierPath *path = [[UIBezierPath alloc] init];
[path moveToPoint:CGPointMake(0, 0)];
[path addLineToPoint:CGPointMake(100, 100)];
// [path moveToPoint:CGPointMake(100, 100)];
[path addLineToPoint:CGPointMake(0, 100)];
// [path moveToPoint:CGPointMake(0, 100)];
[path addLineToPoint:CGPointMake(0, 0)];
[path closePath];
CAShapeLayer *layer = [CAShapeLayer new];
layer.frame = self.contentView.bounds;
layer.path = path.CGPath;
self.layer.mask = layer; // not self.contentView.layer.mask;
}
I have table cells that can expand to show some sub-detail, and I want a downward triangle to appear to let the user knows this is possible.
Here's the code that creates the triangle view...
-(UIView *)triangularViewWithWidth:(CGFloat)width height:(CGFloat)height pointsUp:(BOOL)pointsUp {
UIBezierPath *trianglePath = [UIBezierPath bezierPath];
if(pointsUp){
[trianglePath moveToPoint:CGPointMake(width / 2, 0)];
[trianglePath moveToPoint:CGPointMake(width, height)];
[trianglePath moveToPoint:CGPointMake(0, height)];
[trianglePath closePath];
} else {
[trianglePath moveToPoint:CGPointMake(0, 0)];
[trianglePath moveToPoint:CGPointMake(width, 0)];
[trianglePath moveToPoint:CGPointMake(width / 2, height)];
[trianglePath closePath];
}
CAShapeLayer *l = [CAShapeLayer layer];
l.path = trianglePath.CGPath;
UIView *triangleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
triangleView.layer.mask = l;
return triangleView;
}
and the code the implements it...
-(void)drawTriangle {
if([self.detail.subDetails count]){
self.openCloseTriangle = [self triangularViewWithWidth:100 height:100 pointsUp:NO];
NSLog(#"triangle origin is: %#", NSStringFromCGPoint(self.openCloseTriangle.frame.origin));
NSLog(#"triangle size is: %#", NSStringFromCGSize(self.openCloseTriangle.frame.size));
self.openCloseTriangle.backgroundColor = [UIColor redColor];
[self.contentView addSubview:self.openCloseTriangle];
[self.contentView bringSubviewToFront:self.openCloseTriangle];
}
}
The code works as expected (creates a large red rectangle) if I comment out this line:
l.path = trianglePath.CGPath;
so I suppose there's something I'm not understanding about shape layers, but I'm using basically the same drawing code in another part of my app and it works fine.
The point and rect NSLog output checks out fine, too (even though it doesn't appear):
triangle origin is: {0, 0}
triangle size is: {100, 100}
you must use addLineToPoint: instead of moveToPoint: to create the path:
-(UIView *)triangularViewWithWidth:(CGFloat)width height:(CGFloat)height pointsUp:(BOOL)pointsUp {
UIBezierPath *trianglePath = [UIBezierPath bezierPath];
if(pointsUp){
[trianglePath moveToPoint:CGPointMake(width / 2, 0)];
[trianglePath addLineToPoint:CGPointMake(width, height)];
[trianglePath addLineToPoint:CGPointMake(0, height)];
[trianglePath closePath];
} else {
[trianglePath moveToPoint:CGPointMake(0, 0)];
[trianglePath addLineToPoint:CGPointMake(width, 0)];
[trianglePath addLineToPoint:CGPointMake(width / 2, height)];
[trianglePath closePath];
}
CAShapeLayer *l = [CAShapeLayer layer];
l.path = trianglePath.CGPath;
UIView *triangleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
triangleView.layer.mask = l;
return triangleView;
}
also this line
self.openCloseTriangle = [self triangularViewWithWidth:100 height:100 pointsUp:NO];
is problematic: a view-property usually should be weak, as the view is owned by the view hierarchy, but if you assign the newly created view directly to such a property, it might be deallocated immediately. create a local view, add it to another view, than assign it to the property, that is weak.
my test code
#import "ViewController.h"
#interface ViewController ()
#property (weak) UIView *openCloseTriangle;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self drawTriangle];
}
-(UIView *)triangularViewWithWidth:(CGFloat)width height:(CGFloat)height pointsUp:(BOOL)pointsUp {
UIBezierPath *trianglePath = [UIBezierPath bezierPath];
if(pointsUp){
[trianglePath moveToPoint:CGPointMake(width / 2, 0)];
[trianglePath addLineToPoint:CGPointMake(width, height)];
[trianglePath addLineToPoint:CGPointMake(0, height)];
[trianglePath closePath];
} else {
[trianglePath moveToPoint:CGPointMake(0, 0)];
[trianglePath addLineToPoint:CGPointMake(width, 0)];
[trianglePath addLineToPoint:CGPointMake(width / 2, height)];
[trianglePath closePath];
}
CAShapeLayer *l = [CAShapeLayer layer];
l.path = trianglePath.CGPath;
UIView *triangleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
triangleView.layer.mask = l;
return triangleView;
}
-(void)drawTriangle {
UIView * v= [self triangularViewWithWidth:100 height:100 pointsUp:NO];
self.openCloseTriangle = v;
NSLog(#"triangle origin is: %#", NSStringFromCGPoint(self.openCloseTriangle.frame.origin));
NSLog(#"triangle size is: %#", NSStringFromCGSize(self.openCloseTriangle.frame.size));
self.openCloseTriangle.backgroundColor = [UIColor redColor];
[self.view addSubview:self.openCloseTriangle];
}
#end
Before the thought of posting this question came into my mind I had already spent a few hours on this small snippet of code... So I wrote two different pieces of code in two different classes, and both were used to cut out a specific shape. But for some reason only one of them worked!?!? I have ABSOLUTELY no idea why this happened, for they have the exact same structure.
Below is the snippet that WORKS.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor blackColor];
UIBezierPath *arrowPath = [UIBezierPath bezierPath];
CGPoint start = CGPointMake(self.frame.size.width - 2, self.frame.size.height / 2);
[arrowPath moveToPoint:start];
[arrowPath addLineToPoint:CGPointMake(7, 7)];
[arrowPath addLineToPoint:CGPointMake(2, 7)];
[arrowPath addLineToPoint:CGPointMake(self.frame.size.width - 7, self.frame.size.height/2)];
[arrowPath addLineToPoint:CGPointMake( 2, self.frame.size.height - 7)];
[arrowPath addLineToPoint:CGPointMake(7, self.frame.size.height - 7)];
[arrowPath addLineToPoint:start];
[arrowPath closePath];
UIView *arrow = [[UIView alloc]initWithFrame:self.frame];
arrow.backgroundColor = [UIColor whiteColor];
CAShapeLayer *mask = [CAShapeLayer layer];
mask.frame = self.frame;
mask.backgroundColor = (__bridge CGColorRef)([UIColor clearColor]);
mask.path = arrowPath.CGPath;
arrow.layer.mask = mask;
[self addSubview:arrow];
}
return self;
}
And here is the piece of code that only works PARTIALLY.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIView *roundedRectView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 20)];
roundedRectView.backgroundColor = [UIColor clearColor];
[self addSubview:roundedRectView];
UIBezierPath *roundedRectPath = [UIBezierPath bezierPathWithRoundedRect:roundedRectView.frame cornerRadius:10];
UIView *rectangle = [[UIView alloc]initWithFrame:roundedRectView.frame];
rectangle.backgroundColor = [UIColor blackColor];
CAShapeLayer *mask = [CAShapeLayer layer];
mask.frame = rectangle.frame;
mask.path = roundedRectPath.CGPath;
mask.backgroundColor = (__bridge CGColorRef)([UIColor clearColor]);
rectangle.layer.mask = mask;
rectangle.alpha = 0.8;
[roundedRectView addSubview:rectangle];
UIView *rectangleTwo = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 20, self.frame.size.width, 20)];
rectangleTwo.backgroundColor = [UIColor redColor];
UIBezierPath *trianglePath = [UIBezierPath bezierPath];
CGPoint start = CGPointMake(rectangleTwo.frame.size.width / 2 - 15, 0);
[trianglePath moveToPoint:start];
[trianglePath addLineToPoint:CGPointMake(start.x + 20, start.y)];
[trianglePath addLineToPoint:CGPointMake(start.x + 10, start.y + 20)];
[trianglePath addLineToPoint:start];
[trianglePath closePath];
CAShapeLayer *triangleMask = [CAShapeLayer layer];
triangleMask.frame = rectangleTwo.frame;
triangleMask.path = trianglePath.CGPath;
rectangleTwo.layer.mask = triangleMask;
[self addSubview:rectangleTwo];
}
return self;
}
Allocating this speech bubble using CGRectMake(0,0,80,60), only the rounded rectangle showed up on the screen, but the triangle was no where to be seen.
Can anyone point out what's wrong with this code? Any help is appreciated, thanks!!
Problem solved!
Turned out that my triangleMask.frame wasn't defined properly. Instead of triangleMask.path = rectangleTwo.frame I should've used triangleMask.frame = CGRectMake(0, 0, rectangleTwo.frame.size.width, rectangleTwo.frame.size.height); instead! CAShapeLayer's origin is relative to that of the shape on which you want to place it!
I have a UIView that contains some subviews - drawn using UIBezierPath. Every subview contains a scrollview having image as mask. When I try to render image of whole view (overall), instead of making image from path, it draws image of individual rect of subviews. Let me make it clear by figure:
Original View:
What I am getting after rendering Image:
I don't know what am I missing. Please guide me. Thanks.
Code:
IrregularSquare
#interface IrregularSquare : UIView
{
UIScrollView *imageResizer;
CAShapeLayer *maskLayer;
}
#end
#implementation IrregularSquare
- (id)initWithFrame:(CGRect)frame index:(NSInteger) index
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
self.layer.backgroundColor = [UIColor clearColor].CGColor;
}
return self;
}
-(void) drawRect:(CGRect)rect
{
UIBezierPath *aPath = [UIBezierPath bezierPath];
[aPath moveToPoint:CGPointMake(CGRectGetMinX(rect), CGRectGetMinY(rect))];
[aPath addLineToPoint:CGPointMake(CGRectGetMaxX(rect), CGRectGetMinY(rect))];
[aPath addLineToPoint:CGPointMake(CGRectGetMaxX(rect) - 20, CGRectGetMidY(rect) - 10)];
[aPath addLineToPoint:CGPointMake(CGRectGetMaxX(rect) - 5, CGRectGetMaxY(rect) - 10)];
[aPath addLineToPoint:CGPointMake(CGRectGetMidX(rect) + 10, CGRectGetMaxY(rect))];
[aPath addLineToPoint:CGPointMake(CGRectGetMidX(rect) - 20, CGRectGetMaxY(rect) - 20)];
[aPath addLineToPoint:CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect) - 20)];
[aPath closePath];
aPath.lineWidth = 2;
[[UIColor whiteColor] setStroke];
[aPath stroke];
[self initializeImageWithPath]; //this method adds scrollview to main view and assigns an image to scrollview with mask
}
-(void) initializeImageWithPath
{
CGRect aFrame = somevalue;
self.backImage = [[UIImageView alloc] initWithFrame:aFrame];
self.backImage.contentMode = UIViewContentModeScaleAspectFill;
self.backImage.clipsToBounds = YES;
imageResizer = [[UIScrollView alloc] initWithFrame:self.bounds];
imageResizer.showsHorizontalScrollIndicator = imageResizer.showsVerticalScrollIndicator = NO;
imageResizer.scrollEnabled = YES;
imageResizer.backgroundColor = [UIColor clearColor];
imageResizer.zoomScale = 1.0;
imageResizer.delegate = self;
imageResizer.clipsToBounds = YES;
[imageResizer addSubview:self.backImage];
[self addSubview: imageResizer];
maskLayer = [CAShapeLayer layer];
maskLayer.path = [aPath CGPath];
self.layer.mask = maskLayer;
}
IrregularShapesContainer.m
#implementation IrregularShapesContainer
-(void) drawSubviews
{
for(UIView *aView in self.subviews)
[aView removeFromSuperview];
IrregularSquare *sq1 = [[IrregularSquare alloc] initWithFrame:CGRectMake(HORIPADDING, VERTICALPADDING, (self.frame.size.width - (HORIPADDING * 2) - (SPACING * 2)) * .5, (self.frame.size.height - (VERTICALPADDING) - SPACING) * .5) index:1];
[self addSubview:sq1];
[sq1.layer display];
[self randomlyPlaceImage:sq1];
sq1.irrViewDelegate = self;
//many similar instances are added in following code
}
#end
I hope my Question is clear.. Any help is appreciated.
What appears when I run this thing is a little off on positioning. It is a simple CALayer subclass with a drawInContext function and positioning it in the view controller.
When I create the layer and add it, it is done with this code in the controller code. This runs, so the layer is created. But the position is not correct. It seems to work better in portrait, but I also need ways of detecting the rotation (but that is not my concern immediately).
- (void)viewDidLoad
{
[super viewDidLoad];
Canvas *c = [Canvas layer];
self.canvas = c;
[self.view.layer addSublayer: self.canvas];
//c.frame = self.view.bounds;
self.canvas.anchorPoint = CGPointMake(0, 0);
self.canvas.position = CGPointMake(0, 0);
self.canvas.bounds = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
self.canvas.backgroundColor = [UIColor lightGrayColor].CGColor;
[self.canvas setNeedsDisplay];
}
The main problem seems to be in the actual drawing, the code follows, but it doesn't actually run even when calling needsDisplay.
- (void)drawInContext:(CGContextRef)ctx {
[super drawInContext: ctx];
CGContextSaveGState(ctx);
UIGraphicsPushContext(ctx);
// CGRect b = self.bounds;
[self drawOrigin];
UIGraphicsPopContext();
CGContextRestoreGState(ctx);
}
- (void) drawOrigin {
CGPoint centre = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
UIBezierPath *hl = [UIBezierPath bezierPath];
[hl moveToPoint: centre];
[hl addLineToPoint: CGPointMake(centre.x, 0)];
[hl moveToPoint: centre];
[hl addLineToPoint: CGPointMake(centre.x, self.bounds.size.height)];
[hl moveToPoint: centre];
[hl addLineToPoint: CGPointMake(0, centre.y)];
[hl moveToPoint: centre];
[hl addLineToPoint: CGPointMake(self.bounds.size.width, centre.y)];
[hl stroke];
}
I don't know where else to look for this code. Any ideas or directions are welcome.