如何在 iPhone 的 UIView 中间创建锐边 [英] How to create sharp edge on the middle of UIView in iPhone

查看:62
本文介绍了如何在 iPhone 的 UIView 中间创建锐边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个图像.当用户选择特定图像时,我必须使用顶部有箭头的视图突出显示该图像.它可以通过使用顶部有锐利箭头边缘的图像来解决,但我想使用uiview来实现.如何使用 iPhone 中的核心图形在 UIView 的顶部中间创建一个尖锐的箭头边缘.就像 iPad 中弹出的尖锐箭头边缘一样.

谁能帮帮我.提前致谢.

解决方案

是的,您可以使用 UIView 来做到这一点.使用 UIBezierPath

然后将路径添加到图形上下文中并填充它,使其具有您想要的颜色.

示例代码如下所示:

#define kArrowHeight 50- (void)drawRect:(CGRect)rect{CGContextRef 上下文 = 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(上下文);}

上面的代码会给你一个视图,它在 UIView 中间包含锐边,看起来像弹出框,如下所示.

请注意:

  • 您可以根据需要更改路径.
  • 视图的背景颜色应该是清晰的颜色.

I have multiple images. when user select on a particular images i have to highlight that image with a view which has an arrow on the on the top. It will be solved simply by using an image which have sharp arrow edge on the top but i want to achieve using a uiview. How to create a sharp arrow edge on the top middle of UIView using core graphics in iPhone. like the popover sharp arrow edge in iPad.

can any one help me. Thanks in advance.

解决方案

Yes, you can do this by using UIView. Create a path as you want using UIBezierPath

Then add the path to the graphics context and fill it will colour you want.

Sample code will looks like this :

#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);

}

Above code will give you a view which contain sharp edge on the middle of UIView which looks like popover, which looks like below.

Please note:

  • You can change the path as per your requirement.
  • Background colour of view should be clear colour.

这篇关于如何在 iPhone 的 UIView 中间创建锐边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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