UITableViewCell's subview not appearing - ios

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

Related

CAShapeLayer mask doesn't show up

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;
}

Bezierpath with rounded corners

I am using the following code to show the pointer like triangle view as shown in image below.
UIView *triangleView = [[UIView alloc] initWithFrame:CGRectMake(70.0f,58.0f, self->_createAccountView.frame.size.width ,20.0f)];
UIBezierPath* trianglePath = [UIBezierPath bezierPath];
[trianglePath moveToPoint:CGPointMake(0, 0)];
[trianglePath addLineToPoint:CGPointMake((self->_createAccountView.frame.size.width/7) -8, (self->_createAccountView.frame.size.height/4))];
[trianglePath addLineToPoint:CGPointMake((self->_createAccountView.frame.size.width/5) , 0)];
[trianglePath closePath];
trianglePath.lineJoinStyle = kCGLineJoinRound;
CAShapeLayer *triangleMaskLayer = [CAShapeLayer layer];
[triangleMaskLayer setPath:trianglePath.CGPath];
triangleView.backgroundColor = [UIColor colorWithRed:1.00 green:1.00 blue:1.00 alpha:1.0];
triangleView.layer.mask = triangleMaskLayer;
[self->_AccountView addSubview:triangleView];
This works fine but I want the pointing edge of the triangle view to be rounded. How can make it rounded? Any help is appreciated!
Change the lineJoin to kCALineJoinRound.
triangleMaskLayer.lineJoin = kCALineJoinRound;

iOS - Draw Bezier Path in Subview

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

Need a tip with UIBezierPath. Triangle Shape like Instagram Sign Up View

I'm trying to create a bezier path like the instagram triangle in the picture below, however I must be doing something wrong. The Bezier path does not show!
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
[self drawTriangle];
}
- (IBAction)closeButton:(UIButton *)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)drawTriangle{
UIBezierPath* trianglePath = [UIBezierPath bezierPath];
[trianglePath moveToPoint:CGPointMake(self.signUpButton.center.x, self.signUpButton.frame.origin.y + 30)];
[trianglePath addLineToPoint:CGPointMake(self.signUpButton.center.x - 10, self.imageView.frame.size.height)];
[trianglePath addLineToPoint:CGPointMake(self.signUpButton.center.x + 10, self.imageView.frame.size.height)];
UIColor *fillColor = [UIColor whiteColor];
[fillColor setFill];
UIColor *strokeColor = [UIColor whiteColor];
[strokeColor setStroke];
[trianglePath fill];
[trianglePath stroke];
[trianglePath closePath];
}
Xcode 10 • Swift 4.2
func drawTriangle(size: CGFloat, x: CGFloat, y: CGFloat, up:Bool) {
let triangleLayer = CAShapeLayer()
let trianglePath = UIBezierPath()
trianglePath.move(to: .zero)
trianglePath.addLine(to: CGPoint(x: -size, y: up ? size : -size))
trianglePath.addLine(to: CGPoint(x: size, y: up ? size : -size))
trianglePath.close()
triangleLayer.path = trianglePath.cgPath
triangleLayer.fillColor = UIColor.white.cgColor
triangleLayer.anchorPoint = .zero
triangleLayer.position = CGPoint(x: x, y: y)
triangleLayer.name = "triangle"
view.layer.addSublayer(triangleLayer)
}
drawTriangle(size: 12, x: view.frame.midX/2, y: view.frame.midY, up: true)
drawTriangle(size: 12, x: view.frame.midX, y: view.frame.midY, up: false)
Here is some sample code that I have drawn a triangle for UIButton in my project and working great.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundColor:[UIColor redColor]];
btn.frame = CGRectMake(100, 100, 80, 53);
[self.view addSubview:btn];
UIBezierPath* bezierPath = UIBezierPath.bezierPath;
[bezierPath moveToPoint:CGPointMake(40, 43)];
[bezierPath addLineToPoint:CGPointMake(25, 53)];
[bezierPath addLineToPoint:CGPointMake(55, 53)];
[bezierPath closePath];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.frame = btn.bounds;
shapeLayer.path = bezierPath.CGPath;
shapeLayer.fillColor = [UIColor whiteColor].CGColor;
[btn.layer addSublayer:shapeLayer];
and if you want to remove triangle of previous button when you click on next button then use following code snippet:
NSArray *arr = btn.layer.sublayers;
for (id class in arr) {
if ([class isKindOfClass:[CAShapeLayer class]] ) {
[class removeFromSuperlayer];
}
}

UIBezierPath doesn't work

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!

Resources