如何在IOS中修整可调整大小的UIView的一个角? [英] How to round off one corner of a resizable UIView in IOS?

查看:95
本文介绍了如何在IOS中修整可调整大小的UIView的一个角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码将我的 UIView 的一个角取整:

I'm using this code to round off one corner of my UIView:

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:
    self.view.bounds byRoundingCorners:(UIRectCornerTopLeft) cornerRadii:
    CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path = maskPath.CGPath;
self.view.layer.mask = maskLayer;
self.view.layer.masksToBounds = NO;

只要我没有调整视图的大小,此代码即可工作。如果我放大视图,则不会出现新区域,因为它在蒙版层的边界之外(此蒙版层不会自动根据视图调整大小)。我可以将遮罩制作得尽可能大,但是在iPad上它可能是全屏遮罩,因此我担心使用如此大的遮罩的性能(我将在其中戴多个遮罩) UI)。另外,对于需要将上角(单独)四舍五入的情况,超大尺寸的面罩将不起作用。

This code works, as long as I don't ever resize the view. If I make the view larger, the new area does not appear because it's outside the bounds of the mask layer (this mask layer does not automatically resize itself with the view). I could just make the mask as large as it will ever need to be, but it could be full-screen on the iPad so I'm worried about performance with a mask that big (I'll have more than one of these in my UI). Also, a super-sized mask wouldn't work for the situation where I need the upper right corner (alone) to be rounded off.

是否有更简单,更轻松的方法来实现这一目标?

Is there a simpler, easier way to achieve this?

更新:这是我要实现的目标: http://i.imgur.com/W2AfRBd.png (我想要的圆角在这里用绿色圈出)

Update: here is what I'm trying to achieve: http://i.imgur.com/W2AfRBd.png (the rounded corner I want is circled here in green).

我已经实现了此版本的工作,使用了 UINavigationController 的子类并覆盖了 viewDidLayoutSubviews 像这样:

I have achieved a working version of this, using a subclass of UINavigationController and overriding viewDidLayoutSubviews like so:

- (void)viewDidLayoutSubviews {
    CGRect rect = self.view.bounds;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect 
        byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(8.0, 8.0)];
    self.maskLayer = [CAShapeLayer layer];
    self.maskLayer.frame = rect;
    self.maskLayer.path = maskPath.CGPath;
    self.view.layer.mask = self.maskLayer;
}

然后实例化我的 UINavigationController 使用我的视图控制器的子类,然后将导航控制器视图的框架偏移20px(y),以显示状态栏并保留44px高的导航栏,如图所示。

I then instantiate my UINavigationController subclass with my view controller, and then I offset the frame of the nav controller's view by 20px (y) to expose the status bar and leave a 44-px high navigation bar, as shown in the picture.

代码可以正常工作,只是它不能很好地处理旋转。当应用程序旋转时, viewDidLayoutSubviews 在旋转之前被称为 并且我的代码创建了一个适合旋转 之后的视图的蒙版;这对旋转产生了不良的阻塞性,在旋转期间应隐藏的位会暴露出来。此外,尽管没有使用此蒙版,应用程序的旋转也非常流畅,但是在创建蒙版后,旋转变得非常生涩且缓慢。

The code is working, except that it doesn't handle rotation very well at all. When the app rotates, viewDidLayoutSubviews gets called before the rotation and my code creates a mask that fits the view after rotation; this creates an undesirable blockiness to the rotation, where bits that should be hidden are exposed during the rotation. Also, whereas the app's rotation is perfectly smooth without this mask, with the mask being created the rotation becomes noticeably jerky and slow.

iPad应用程序Evomail也具有圆角,例如

The iPad app Evomail also has rounded corners like this, and their app suffers from the same problem.

推荐答案

我知道这是一种很简单的方法,但是不能您是否只是在角落的顶部添加了png?

I know this is a pretty hacky way of doing it but couldn't you just add a png over the top of the corner?

我知道这很丑,但这不会影响性能,如果它具有子视图并且用户不会注意到,则旋转会很好。

Ugly I know, but it won't affect performance, rotation will be fine if its a subview and users won't notice.

这篇关于如何在IOS中修整可调整大小的UIView的一个角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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