圆形的UIView底边

时间:2013-05-14 14:09:09

标签: uiview uibezierpath

在App中我已经将UINavigationBar子类化了,并且作为我的TitleView我正在设置(在View Controller中)自定义UIView

[self.playButton setBackgroundColor:[UIColor yellowColor]];
[self.navigationItem setTitleView:self.playButton];

此视图的底边(playButton)应该是弯曲的。不仅角落,机器人整个底部边缘。为了达到这个效果我正在使用这个代码:

CAShapeLayer *maskLayer = [CAShapeLayer layer];
UIBezierPath *path = [[UIBezierPath alloc] init];

[path addCurveToPoint:CGPointMake(self.playButton.frame.size.width, self.playButton.frame.size.height)  controlPoint1:CGPointMake(0, self.playButton.frame.size.height) controlPoint2:CGPointMake(self.playButton.frame.size.width, 60)];
maskLayer.path = path.CGPath;
self.playButton.layer.mask = maskLayer;

但在那段代码之后,我的UIView(playButton)消失了!这是应用UIView弯曲底边的正确方法吗?

它应如下所示:http://i.imgur.com/51UIQsE.png

2 个答案:

答案 0 :(得分:0)

CAShapeLayer *maskLayer = [CAShapeLayer layer];
[maskLayer setFrame:self.playButton.bounds];
UIBezierPath *path = [UIBezierPath bezierPath];

[path addArcWithCenter:CGPointMake(self.playButton.center.x, -36.0) radius:65.5 startAngle:0 endAngle:2*M_PI clockwise:YES];


//[path closePath];
maskLayer.path = path.CGPath;
self.playButton.layer.mask = maskLayer;

答案 1 :(得分:0)

解决方案:

-(void)roundCorners:(UIRectCorner)corner radius:(float)radius
{   
    _cornerRadius = radius;
    _corner = corner;
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    [super drawRect:rect];

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:_corner cornerRadii:CGSizeMake(_cornerRadius, _cornerRadius)];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;

    self.layer.mask = maskLayer;
}