如何在iPhone的UIView的中间创build尖锐的边缘

我有多个图像。 当用户select一个特定的图像,我必须突出显示该图像的顶部有一个箭头的视图。 这将简单地通过使用在顶部具有尖锐箭头边缘的图像来解决,但是我想使用uview来实现。 如何使用iPhone中的核心graphics在UIView的顶部中间创build一个尖锐的箭头边缘。 就像iPad上的popup式箭头一样。

谁能帮我。 提前致谢。

是的,你可以通过使用UIView来做到这一点。 根据需要使用UIBezierPath创build一个path

然后添加graphics上下文的path,并填充它将你想要的颜色。

示例代码如下所示:

 #define kArrowHeight 50 - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); UIBezierPath *fillPath = [UIBezierPath bezierPath]; [fillPath moveToPoint:CGPointMake(0, self.bounds.origin.y+kArrowHeight)]; [fillPath addLineToPoint:CGPointMake(self.bounds.size.width/2-(kArrowHeight/2), kArrowHeight)]; [fillPath addLineToPoint:CGPointMake(self.bounds.size.width/2, 0)]; [fillPath addLineToPoint:CGPointMake(self.bounds.size.width/2+(kArrowHeight/2), kArrowHeight)]; [fillPath addLineToPoint:CGPointMake(self.bounds.size.width, kArrowHeight)]; [fillPath addLineToPoint:CGPointMake(self.bounds.size.width, self.bounds.size.height)]; [fillPath addLineToPoint:CGPointMake(0, self.bounds.size.height)]; [fillPath closePath]; CGContextAddPath(context, fillPath.CGPath); CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor); CGContextFillPath(context); } 

上面的代码会给你一个视图,其中包含UIView中间的尖锐边缘,看起来像popup窗口,如下所示。

在这里输入图像说明

请注意:

  • 您可以根据您的要求更改path。
  • 背景的颜色应该是清晰的颜色。

如果它的iPad的应用程序,你可以使用uipopovercontroller。 除此之外,你可以在github上find各种控件

您可以使用UIView作为容器,并将UIView背景设置为透明。 接下来,将一个子UIView添加到容器中,并在顶部边缘添加一个箭头(UIImageView)。