Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triangular UIView or UIImageView

I have requirement like below image.

enter image description here

But, i'm confused about how to achieve this? Can i achieved it by using 3 UIImageViews or UIViews. If both then, which one is better? Finally, i have to combine three images & make one from that three images. I should be able to get touch of image too. I have no idea about this. Thanks.

like image 583
Akshit Zaveri Avatar asked Feb 14 '14 10:02

Akshit Zaveri


People also ask

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

What is use of UIImageView?

Overview. Image views let you efficiently draw any image that can be specified using a UIImage object. For example, you can use the UIImageView class to display the contents of many standard image files, such as JPEG and PNG files.

How do you make a triangle view in Swift?

Swift 2: class ViewController: UIViewController { override func viewDidLoad() { super. viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let triangle = TriangleView(frame: CGRectMake(10, 20, 25, 30)) triangle.

How do I install UIImageView?

First you create a UIImage from your image file, then create a UIImageView from that: let imageName = "yourImage. png" let image = UIImage(named: imageName) let imageView = UIImageView(image: image!)


1 Answers

Every UIView has a backing CALayer (accessible by aview.layer).

Every CALayer has a mask property, which is another CALayer. A mask allows to define a see-through shape for the layer, like a stencil.

So you need three UIImageViews, each of them has a different .layer.mask, each of these masks is a different CAShapeLayer whose .path are triangular CGPaths.

// Build a triangular path UIBezierPath *path = [UIBezierPath new]; [path moveToPoint:(CGPoint){0, 0}]; [path addLineToPoint:(CGPoint){40, 40}]; [path addLineToPoint:(CGPoint){100, 0}]; [path addLineToPoint:(CGPoint){0, 0}];  // Create a CAShapeLayer with this triangular path // Same size as the original imageView CAShapeLayer *mask = [CAShapeLayer new]; mask.frame = imageView.bounds; mask.path = path.CGPath;  // Mask the imageView's layer with this shape imageView.layer.mask = mask; 

Repeat three times.

like image 91
Cyrille Avatar answered Oct 04 '22 03:10

Cyrille