ios – 我怎样才能在UIView上制作弯角?

前端之家收集整理的这篇文章主要介绍了ios – 我怎样才能在UIView上制作弯角?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这里有很多关于使UIView的角落四舍五入的问题.不幸的是,我找不到任何关于如何制作弯角的东西.如何制作一个角度为45度角的UIView呢?

如果你能告诉我如何以一个角度切割任何单个角落,你甚至可以获得奖金(象征性)金星.

编辑:作为参考,here’s a link到我怀疑有一个与此解决方案类似的实现的问题.我只是不知道我要改变什么.

解决方法

首先,您需要一个带角度角的路径:
- (CGPathRef) makeAnglePathWithRect: (CGRect)rect withSize:(float) s {

    CGPoint one = CGPointMake( rect.origin.x +s,rect.origin.y);
    CGPoint two = CGPointMake( rect.origin.x + rect.size.width - s,rect.origin.y);

    CGPoint three = CGPointMake( rect.origin.x + rect.size.width,rect.origin.y +s);
    CGPoint four = CGPointMake( rect.origin.x + rect.size.width,rect.origin.y + rect.size.height -s);

    CGPoint five = CGPointMake( rect.origin.x + rect.size.width-s,rect.origin.y + rect.size.height);
    CGPoint six = CGPointMake(rect.origin.x+s,rect.origin.y + rect.size.height);

    CGPoint seven = CGPointMake(rect.origin.x,rect.origin.y + rect.size.height-s);
    CGPoint eight = CGPointMake(rect.origin.x,rect.origin.y + s);

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path,NULL,one.x,one.y);
    CGPathAddLineToPoint(path,two.x,two.y);
    CGPathAddLineToPoint(path,three.x,three.y);
    CGPathAddLineToPoint(path,four.x,four.y);
    CGPathAddLineToPoint(path,five.x,five.y);
    CGPathAddLineToPoint(path,six.x,six.y);
    CGPathAddLineToPoint(path,seven.x,seven.y);
    CGPathAddLineToPoint(path,eight.x,eight.y);
    CGPathAddLineToPoint(path,one.y);

    return path;
}

然后,您需要使用路径指定掩码:

CAShapeLayer *maskLayer = [CAShapeLayer layer];
CGRect bounds = CGRectMake(0.0f,0.0f,100,100); //figure out your bounds

[maskLayer setFrame:bounds];
CGPathRef p = [self makeAnglePathWithRect:bounds withSize:20.0];
maskLayer.path = p;
_myview.layer.mask = maskLayer;

如果要从任何角落移除角度,请使用第1-8点来移除“s”值.您可以使用size参数更改从角落切出的三角形的大小.

猜你在找的iOS相关文章