How to make a CGRect slashed at bottom? - ios

I'm trying to implement the following:
A custom NavigationBar which is slashed at the bottom, how can I do this using CGRect?
Another alternative would be to make a image of it and push the list up to overlap the transparent part in the image. I'd prefer the first solution if possible tho. Any suggestions?
This is my current WIP state:

You can use Bezier path
Just add lines and make your custom shape
like this:
let path = UIBezierPath()
shape.move(to: CGPoint(x: 0, y: 0))
shape.addLine(to: CGPoint(x: 0, y: 320))
shape.addLine(to: CGPoint(x: 94, y: 320))
shape.addLine(to: CGPoint(x: 64, y: 0))
shape.close()

Related

update point after transform

I'm trying to change a view position using CGAffineTransform but the transform statement doesn't affect it.
For example, let's call the view that moves movingView:
At the start, movingView is at point CGPoint(x: 100, y: 100)
then I'm moving movingView by this code :
movingView.transform = CGAffineTransform(translationX: 100, y: 300)
now if I do : print(movingView.layer.position), I still get
CGPoint(x: 100, y:100)
instead of
CGPoint(x: 200, y: 400)
Applying a transform to a view (or layer) doesn't update its center location (or position). It changes how and where the view (or layer) appear on screen but not the values of its center, (or position), or bounds properties.
If you want to know what how the point is affected by a translation, then you can compute a new point by applying the transform to it:
let point = CGPoint(x: 100, y: 200)
let transform = CGAffineTransform(translationX: 20, y: 40)
let transformedPoint = point.applying(transform) // x: 120, y: 240
Actually What changes is the frame of View if the view is added to a SuperView.
Check : movingView.frame.origin.
The transform of CALayer of UIView has already changed:
CGPoint.init(x: movingView.layer.transform.m41, y: movingView.layer.transform.m42)
So the transform of view is based on transform of CALayer.

Create an image in code and display it into UIImageView

I am trying to create an image. By checking the size, I can see that image has been created. But I am unable to assign that image as image property of UIImageView. Please help.
let imageRenderer=UIGraphicsImageRenderer(size: self.view.bounds.size)
book = imageRenderer.image { c in
let ctx=c.cgContext
ctx.setStrokeColor(UIColor.black.cgColor)
ctx.setLineWidth(5)
let x=self.view.bounds.maxX
let y=self.view.bounds.maxY
ctx.move(to: CGPoint(x:x/16,y:x/8))
ctx.addLine(to: CGPoint(x: x/2, y: x/4))
ctx.addLine(to: CGPoint(x: x-x/16, y: x/8))
ctx.addLine(to:CGPoint(x: x-x/16, y: y-x/8))
ctx.addLine(to: CGPoint(x: x/2, y: y-x/16))
ctx.addLine(to: CGPoint(x: x/16, y: y-x/8))
ctx.closePath()
}
self.image.image = book!
You just add lines but not paint color for them.
You can pain color for them by calling
ctx.strokePath()
right after (inside the closure)
ctx.closePath()
More information you can read here
https://developer.apple.com/documentation/coregraphics/cgcontext/1454490-strokepath

How to set shadow at left right and bottom in UIView in ios Swift?

I want shadow at left, right and bottom in view. But Swift 3 shadow offset provide only height or width. I found similar answer see below link it show shadow top left right shadow. I want left right and bottom.
see link little similar answer
It's almost the same task as link provided, just change the BezierPath.
let block1 = UIView(frame: CGRect.init(x: 50, y: 50, width: 300, height: 300))
block1.backgroundColor = UIColor.red
block1.layer.masksToBounds = false
block1.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
block1.layer.shadowRadius = 5.0
block1.layer.shadowOpacity = 0.7
let path = UIBezierPath()
path.move(to: CGPoint(x: 0.0, y: 0.0))
// Move to center between two top points instead of bottom
path.addLine(to: CGPoint(x: block1.frame.size.width/2.0, y: block1.frame.size.height/2.0))
path.addLine(to: CGPoint(x: block1.frame.size.width, y: 0.0))
path.addLine(to: CGPoint(x: block1.frame.size.width, y: block1.frame.size.height))
path.addLine(to: CGPoint(x: 0.0, y: block1.frame.size.height))
path.close()
block1.layer.shadowPath = path.cgPath

How do I draw a triangle with a corner radius?

I am trying to draw a triangle like this one exactly
Can someone guide me how to achieve this as I tried a lot with no success?
my worst case scenario is to use a image but I don't want to do that.
Thanks a lot!
Maybe you should try this:
let trianglePath = UIBezierPath()
trianglePath.lineJoinStyle = .round
trianglePath.lineWidth = 25
trianglePath.move(to: CGPoint(x: 5.0, y: 10.0))
trianglePath.addLine(to: CGPoint(x: 130, y: 280.0))
trianglePath.addLine(to: CGPoint(x: 265.0, y: 10.0))
trianglePath.close()
That gives you this in Playground:

Bezier path with line width on one side?

I have two images, and I want to have the left one clipped so it looks like they are placed with a diagonal line in between. Like so:
I'm able to create the path and clip, but cannot figure out how to set the line width for just the right part...
My code:
let path = UIBezierPath()
path.moveToPoint(CGPoint(x: 0, y: 0))
path.addLineToPoint(CGPoint(x: leftContentView.frame.width, y: 0))
path.addLineToPoint(CGPoint(x: leftContentView.frame.width - 20, y: leftContentView.frame.height))
path.addLineToPoint(CGPoint(x: 0, y: leftContentView.frame.height))
path.closePath()
let mask = CAShapeLayer()
mask.path = path.CGPath
self.leftContentView.layer.mask = mask
Just use no line and add an additional layer representing the line with the appropriate implied width.

Resources