iOS-记录实现渐变色和圆角不同角度切割的办法
一、渐变色
- 找的几个方法,都是需要先确定fram值的,先用着吧,其实最快捷方式还是弄个图片省事
在view的类方法里扩展,通过绘制渐变色生成图片,然后设置背景色。
typedef enum : NSUInteger {
TmyCustAxisNeither = 0,
TmyCustAxisHorizontal = 1 << 0,
TmyCustAxisVertical = 1 << 1,
TmyCustAxisBoth = (TmyCustAxisHorizontal | TmyCustAxisVertical),
} TmyCustAxis;
- (void)tmyCustLineHChangeColor:(UIColor *)color toColor:(UIColor *)toColor withDir:(TmyCustAxis)changeDirection {
if (CGRectEqualToRect(self.frame, CGRectZero) || self.frame.size.height < 1) {
return;
}
CAGradientLayer *gradientL = [CAGradientLayer layer];
if (changeDirection == TmyCustAxisHorizontal) {
gradientL.startPoint = CGPointMake(0, 0);
gradientL.endPoint = CGPointMake(1, 0);
}else if (changeDirection == TmyCustAxisVertical) {
gradientL.startPoint = CGPointMake(0, 0);
gradientL.endPoint = CGPointMake(0, 1);
}else if (changeDirection == TmyCustAxisBoth) {
gradientL.startPoint = CGPointMake(0, 0);
gradientL.endPoint = CGPointMake(1, 1);
}else {
gradientL.startPoint = CGPointMake(0, 1);
gradientL.endPoint = CGPointMake(1, 0);
}
gradientL.frame = self.bounds;
gradientL.colors = @[(id)color.CGColor, (id)toColor.CGColor];
UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0);
[gradientL renderInContext:UIGraphicsGetCurrentContext()];
UIImage *tmpImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.backgroundColor = [UIColor colorWithPatternImage:tmpImg];
}
二、圆角切割
设置不同的圆角
rectCorner
:每个圆角的半径值左上top 右上left 左下bottom 右下right
radView
:要设置的view
- (void)qiHandleFourConnerWithView:(UIView *)radView edge:(UIEdgeInsets)rectCorner {
// 左上top 右上left 左下bottom 右下right
CGPathRef pathR = [self qiHandlePathWithRect:radView.bounds CornerRect:rectCorner];
CAShapeLayer *shaperLayer = [[CAShapeLayer alloc] init];
shaperLayer.frame = radView.bounds;
shaperLayer.path = pathR;
radView.layer.mask = shaperLayer;
}
/// 四个不同角度的圆角
/// @param rect 当前view 的 rect
/// @param cornerRect 圆角的半径rect。 UIEdgeInsets 对应:top:左上; left:右上; bottom:左下; right:右下;
- (CGPathRef)qiHandlePathWithRect:(CGRect)rect CornerRect:(UIEdgeInsets)cornerRect {
CGFloat minX = CGRectGetMinX(rect);
CGFloat minY = CGRectGetMinY(rect);
CGFloat maxX = CGRectGetMaxX(rect);
CGFloat maxY = CGRectGetMaxY(rect);
// top 左上 left 右上 bottom 左下 right 右下 圆角的半径
// 4个角的圆心
CGPoint topLeftPoint = CGPointMake(minX cornerRect.top, minY cornerRect.top); // 左上角
CGPoint topRightPoint = CGPointMake(maxX - cornerRect.left, minY cornerRect.left); // 右上
CGPoint bottomLeftPoint = CGPointMake(minX cornerRect.bottom, maxY - cornerRect.bottom);// 左下角
CGPoint bottomRightPoint = CGPointMake(maxX - cornerRect.right, maxY - cornerRect.right);// 右下角
// fales理解为从顺时针方向开始
CGMutablePathRef mutRef = CGPathCreateMutable();
CGPathAddArc(mutRef, nil, topLeftPoint.x, topLeftPoint.y, cornerRect.top, M_PI, 3*M_PI_2, false);
CGPathAddArc(mutRef, nil, topRightPoint.x, topRightPoint.y, cornerRect.left, 3*M_PI_2, 0, false);
CGPathAddArc(mutRef, nil, bottomRightPoint.x, bottomRightPoint.y, cornerRect.right, 0, M_PI_2, false);
CGPathAddArc(mutRef, nil, bottomLeftPoint.x, bottomLeftPoint.y, cornerRect.bottom, M_PI_2, M_PI, false);
CGPathCloseSubpath(mutRef);
return mutRef;
}
- 起始角度理解
调用方式:
[self qiHandleFourConnerWithView:self.mallTagImgView edge:UIEdgeInsetsMake(8, 2, 0, 13)];
- 实际效果
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhifkkfc
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
excel下划线不显示怎么办
PHP中文网 06-23 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
微信运动停用后别人还能看到步数吗
PHP中文网 07-22 -
excel图片置于文字下方的方法
PHP中文网 06-27