如何在UIImageView中添加箭头?

时间:2015-10-28 13:43:11

标签: ios uiview uiimageview uiimage

我必须在UIImageView中使用箭头。它可能就像UIImageView中的透明空间一样。附上样本图像并用黄色框突出显示。 enter image description here

1 个答案:

答案 0 :(得分:6)

使用遮罩,例如您有200 * 100的图像 enter image description here

代码

CAShapeLayer * shapeLayer = [CAShapeLayer layer];
UIBezierPath * bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint:CGPointMake(0, 0)];
[bezierPath addLineToPoint:CGPointMake(0,100)];
[bezierPath addLineToPoint:CGPointMake(10,100)];
[bezierPath addLineToPoint:CGPointMake(20,90)];
[bezierPath addLineToPoint:CGPointMake(30, 100)];
[bezierPath addLineToPoint:CGPointMake(200,100)];
[bezierPath addLineToPoint:CGPointMake(200, 0)];
[bezierPath closePath];
shapeLayer.path = bezierPath.CGPath;
self.imageview.layer.mask = shapeLayer;

注意:剪辑部分仍然可以作为Imageview的一部分捕获触摸事件或手势,您可以将pointInside重写为忽略触摸事件

编辑关于其工作原理

你只需要创建你喜欢的任何形状,我只是创建一个像这样的形状的代码,然后用这个形状来设置面具

所以,如果你想制作一个右箭头,只需创建一个这样的形状

CAShapeLayer * shapeLayer = [CAShapeLayer layer];
UIBezierPath * bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint:CGPointMake(0, 0)];
[bezierPath addLineToPoint:CGPointMake(0,100)];
[bezierPath addLineToPoint:CGPointMake(170,100)];
[bezierPath addLineToPoint:CGPointMake(180,90)];
[bezierPath addLineToPoint:CGPointMake(190, 100)];
[bezierPath addLineToPoint:CGPointMake(200,100)];
[bezierPath addLineToPoint:CGPointMake(200, 0)];
[bezierPath closePath];
shapeLayer.path = bezierPath.CGPath;

形状截图

所以,您需要的是

  • 找出你想要的形状(轮廓蒙版形状)
  • 使用bezierPath绘制
  • 设置面具