三角UIView或UIImageView [英] Triangular UIView or UIImageView
问题描述
我的要求如下图所示。
但是,我对如何实现这一点很困惑?我可以通过使用3 UIImageViews
或 UIViews
来实现它。如果两者都有,哪一个更好?最后,我必须结合三个图像和从这三张图片中选出一张。我也应该能够触摸到图像。我不知道这个。谢谢。
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
有一个掩码
属性,这是另一个 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
s,它们每个都有不同的 .layer.mask
,这些掩码中的每一个都是不同的 CAShapeLayer
其 .path
是三角形 CGPath
s。
So you need three UIImageView
s, each of them has a different .layer.mask
, each of these masks is a different CAShapeLayer
whose .path
are triangular CGPath
s.
// 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屋!