三角形 UIView 或 UIImageView [英] Triangular UIView or UIImageView

查看:24
本文介绍了三角形 UIView 或 UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下图所示的要求.

I have requirement like below image.

但是,我对如何实现这一目标感到困惑?我可以通过使用 3 个 UIImageViewsUIViews 来实现它吗?如果两者都有,哪个更好?最后,我必须结合三个图像 &从那三个图像中制作一个.我也应该能够触摸到图像.我不知道这个.谢谢.

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.

推荐答案

每个 UIView 都有一个支持 CALayer(可通过 aview.layer 访问).

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

每个CALayer 都有一个mask 属性,它是另一个CALayer.蒙版允许为图层定义透明形状,如模板.

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.

所以你需要三个UIImageView,每个都有不同的.layer.mask,每一个mask都是不同的CAShapeLayer其中 .path 是三角形 CGPaths.

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;

重复三遍.

这篇关于三角形 UIView 或 UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆