Create a UIView with only one diagonal side

I need to create a UIView whose left border is tilted from 45 degrees I was wondering if there is a way to do this programmatically? Can CATransform3D help me in this case, since it is not a "three-dimensional rotation"?

Edit

Here is an image explaining more of my necessary output

enter image description here

+8
ios swift uiview cashapelayer diagonal
source share
2 answers

If you need a form without content, you can create a CAShapeLayer and add it to your layer. (In fact, you can also post content there using this method, but you need to change it a bit).

 CAShapeLayer *layer = [CAShapeLayer layer]; UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:CGPointMake(0, 100)]; // bottom left corner [path addLineToPoint:CGPointMake(100, 0)]; // top middle [path addLineToPoint:CGPointMake(300, 0)]; // top right corner [path addLineToPoint:CGPointMake(300, 100)]; // bottom right corner [path closePath]; layer.path = path.CGPath; layer.fillColor = [UIColor blackColor].CGColor; layer.strokeColor = nil; [theView.layer addSubLayer:layer]; 

This does not require the use of drawRect or anything else. You will need to change the coordinates based on the desired values.

You can also subclass UIView and override drawRect . It takes more work, but UIBezierPath will be pretty much the same.

CALayer very powerful and much used by Apple. For example, the editing canvas on Pages is written almost exclusively using CALayer s.

+12
source share

The shortest way to achieve this is probably to take the following steps:

  • Subclass UIView
  • Override drawRect to provide custom drawing for presentation
  • In drawRect use the Core Graphics API , which allows you to draw custom shapes in the drawing context and offers features such as rotation or scaling

Below is an overview of the API from Apple docs.

0
source share

All Articles