日常我们使用layer的两个属性,简单的两行代码就能实现圆角的呈现 imageView.layer.masksToBounds = YES; imageView.layer.cornerRadius = /** * @brief clip the cornerRadius with image, UIImageView must be setFrame before, no off-screen-rendered */ - (void)zy_cornerRadiusWithImage:(UIImage *)image cornerRadius:(CGFloat)cornerRadius rectCornerType self.bounds.size; CGFloat scale = [UIScreen mainScreen].scale; CGSize cornerRadii = CGSizeMake(cornerRadius , cornerRadius); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0
在这里插入图片描述 I 、 iOS设置视图的cornerRadius属性失效的解决方案 1.1 解决步骤 1、尝试设置_numberLab.clipsToBounds = YES;2、尝试设置 [self.numberLab layoutIfNeeded]; 之后再执行cornerRadius 在设置完约束后, 并不能马上得到它的frame, 只要添加[self.view layoutIfNeeded]; 就能拿到frame layoutIfNeeded]; [self.contentView bringSubviewToFront:self.numberLab]; self.numberLab.layer.cornerRadius 它有什么作用 masksToBounds指在设置子layer在超出父layer时是否被裁剪,YES表示裁剪,NO表示不裁剪,默认是NO;通常在通过设置layer.cornerRadius属性实现圆角效果时要设置 maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius
; @end UIView+Border&CornerRadius.m #import "UIView+Border&CornerRadius.h" @implementation UIView(border_cornerRadius 可视化视图传入的值 */ @dynamic cornerRadius; - (void)setCornerRadius:(CGFloat)cornerRadius { self.layer.cornerRadius = cornerRadius; self.layer.masksToBounds = cornerRadius > 0; } @end Swift下的实现方式(扩展) UIView+Border &CornerRadius.swift import Foundation import UIKit extension UIView { @IBInspectable var cornerRadius 可视化视图传入的值 */ - (void)setCornerRadius:(CGFloat)cornerRadius { self.layer.cornerRadius = cornerRadius
#import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @interface UIView (CornerRadius) /*设置顶部圆角*/ - (void )setCornerOnTop:(CGFloat )cornerRadius ; /*设置底部圆角*/ - (void)setCornerOnBottom:(CGFloat )cornerRadius ; /*设置左边圆角*/ - (void)setCornerOnLeft:(CGFloat )cornerRadius; /*设置右边圆角*/ - (void)setCornerOnRight:(CGFloat )cornerRadius; /*设置四边圆角*/ - (void)setAllCorner; @end NS_ASSUME_NONNULL_END #import "UIView+CornerRadius.h " @implementation UIView (CornerRadius) /*设置顶部圆角*/ - (void)setCornerOnTop:(CGFloat )cornerRadius {
= new CornerRadius(0); Max_Top_CornerRadius = new CornerRadius(0); = new CornerRadius(0, 10, 0, 0); Max_Top_CornerRadius = new CornerRadius(10, 10, } private CornerRadius max_Top_CornerRadius=new CornerRadius(10,10,0,0); /// <summary } } private CornerRadius max_Bottom_CornerRadius = new CornerRadius(0,0,10,10); } } private CornerRadius max_Close_CornerRadius = new CornerRadius(0, 10, 0, 0);
MainPos":27,"SpecialOne":"A","SpecialOnePos":0,"SpecialTwo":"A","SpecialTwoPos":0,"colorhex":"#FF000000","cornerRadius MainPos":28,"SpecialOne":"A","SpecialOnePos":0,"SpecialTwo":"A","SpecialTwoPos":0,"colorhex":"#FF000000","cornerRadius MainPos":29,"SpecialOne":"A","SpecialOnePos":0,"SpecialTwo":"A","SpecialTwoPos":0,"colorhex":"#FF000000","cornerRadius MainPos":30,"SpecialOne":"A","SpecialOnePos":0,"SpecialTwo":"A","SpecialTwoPos":0,"colorhex":"#FF000000","cornerRadius MainPos":31,"SpecialOne":"A","SpecialOnePos":0,"SpecialTwo":"A","SpecialTwoPos":0,"colorhex":"#FF000000","cornerRadius
(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90); 14 * 2, cornerRadius * 2, cornerRadius * 2, 0, 90); 16 path.AddLine(rect.Right - cornerRadius - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90); 21 path.AddLine(rect.X, * 2, cornerRadius * 2, cornerRadius * 2, 0, 90); 144 path.AddLine(rect.Right - cornerRadius , rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90); 149 path.AddLine
, CornerRadius, CornerRadius, CornerRadius)) //圆角矩形 { using (Pen p = new Pen(this.ButtonColor)) { , CornerRadius, CornerRadius, CornerRadius)) { using (Pen p = new Pen(this.HighlightColor)) { g.DrawPath , CornerRadius, CornerRadius, CornerRadius)) { using (SolidBrush sb = new SolidBrush(this.BaseColor , CornerRadius, CornerRadius, CornerRadius), true); using (GraphicsPath gp = RoundRect(new Rectangle (r.Left + 1, r.Top + 1, r.Width - 3, r.Height / 2 - 2), CornerRadius, CornerRadius, CornerRadius, CornerRadius
3.为了让过渡效果更加舒服,这里添加了两个渐变效果,一个是color,从fab的颜色到dialog的背景颜色的渐变;另一个是cornerRadius,即圆角幅度的渐变。 关于属性动画可以看以前的读书笔记,重要代码如下: /** * 形态和颜色可以发生变化的Drawable,形态变化是通过cornerRadius来实现的,颜色变化是通过paint的color来实现的 ) { this.cornerRadius = cornerRadius; paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(color); } public float getCornerRadius() { return cornerRadius; } public void setCornerRadius(float cornerRadius) { this.cornerRadius = cornerRadius;
recognizer.view.superview bringSubviewToFront:recognizer.view]; CGPoint center = recognizer.view.center; CGFloat cornerRadius slideFactor), center.y + (velocity.y * slideFactor)); //限制最小[cornerRadius ]和最大边界值[self.view.bounds.size.width - cornerRadius],以免拖动出屏幕界限 finalPoint.x = MIN(MAX(finalPoint.x , cornerRadius), self.view.bounds.size.width - cornerRadius); finalPoint.y = MIN(MAX(finalPoint.y , cornerRadius), self.view.bounds.size.height - cornerRadius); //使用 UIView 动画使 view 滑行到终点 [
, _cornerRadius, 180f, 90f); 16 graphicsPath.AddArc(clientRectangle.Width - _cornerRadius - _cornerRadius - 1, clientRectangle.Height - _cornerRadius - 1, _cornerRadius, _cornerRadius, 0f, 90f ); 18 graphicsPath.AddArc(0, clientRectangle.Height - _cornerRadius - 1, _cornerRadius - _cornerRadius - 1, clientRectangle.Height - _cornerRadius - 1, _cornerRadius, _cornerRadius, 0f, 90f ); 149 graphicsPath.AddArc(0, clientRectangle.Height - _cornerRadius - 1, _cornerRadius
= null) { double top = Math.Max(CornerRadius.TopLeft, CornerRadius.TopRight ); double bottom = Math.Max(CornerRadius.BottomLeft, CornerRadius.BottomRight); new Rect(0, 0, halfWidth, halfHeight)) ); } if (CornerRadius.TopRight (new Rect(halfWidth, 0, halfWidth, halfHeight))); } if (CornerRadius.BottomLeft BorderBrush="#409DFE" BorderThickness="2" CornerRadius
如下代码: self.myButton.layer.cornerRadius = 20; self.myButton.layer.masksToBounds = YES; self.myButton.layer.borderWidth 设置圆角、描边的Key为: layer.borderWidth layer.borderColorFromUIColor layer.cornerRadius clipsToBounds 这样做不用关联出属性 IB_DESIGNABLE @interface UIView (Inspectable) @property(nonatomic,assign) IBInspectable CGFloat cornerRadius { self.layer.masksToBounds = YES; self.layer.cornerRadius = cornerRadius; } -(void)setBorderColor { return self.layer.cornerRadius; } - (CGFloat)borderWidth{ return self.layer.borderWidth; }
= 2; 68 69 path.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);/ path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius 111 path.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius 127 path.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius *
如下代码: self.myButton.layer.cornerRadius = 20; self.myButton.layer.masksToBounds = YES; self.myButton.layer.borderWidth (不仅仅是圆角、描边~) 设置圆角、描边的Key为: layer.borderWidth layer.borderColorFromUIColor layer.cornerRadius clipsToBounds IB_DESIGNABLE @interface UIView (Inspectable) @property(nonatomic,assign) IBInspectable CGFloat cornerRadius { self.layer.masksToBounds = YES; self.layer.cornerRadius = cornerRadius; } -(void)setBorderColor { return self.layer.cornerRadius; } - (CGFloat)borderWidth{ return self.layer.borderWidth; }
= null) { var top = Math.Max(CornerRadius.TopLeft, CornerRadius.TopRight) ; var bottom = Math.Max(CornerRadius.BottomLeft, CornerRadius.BottomRight); new Rect(0, 0, halfWidth, halfHeight)) ); } if (CornerRadius.TopRight (new Rect(halfWidth, 0, halfWidth, halfHeight))); } if (CornerRadius.BottomLeft (new Rect(0, halfHeight, halfWidth, halfHeight))); } if (CornerRadius.BottomRight
代码比较少,直接列出: public class RoundedBitmapDisplayer implements BitmapDisplayer { protected final int cornerRadius ; } public RoundedBitmapDisplayer(int cornerRadiusPixels, int marginPixels) { this.cornerRadius , int margin) { this.cornerRadius = cornerRadius; this.margin = margin; , cornerRadius, paint); } @Override public int getOpacity() { return RoundedDrawable 作为Drawable的子类,首先关心下它的draw方法: canvas.drawRoundRect(mRect, cornerRadius, cornerRadius,
method void setCircular(boolean circular) : Sets the image shape to circular. void setCornerRadius(float cornerRadius setCircular(boolean circular) : 把图片的形状设为圆形; setCornerRadius(float cornerRadius) : 设置图片的圆角半径。 corner radius to be applied when drawing the bitmap. */ public void setCornerRadius(float cornerRadius (cornerRadius)) { mPaint.setShader(mBitmapShader); } else { mPaint.setShader (null); } mCornerRadius = cornerRadius; invalidateSelf(); } 至于具体的实现,阅读源码发现官方使用了
recognizer.view.superview bringSubviewToFront:recognizer.view]; CGPoint center = recognizer.view.center; CGFloat cornerRadius slideFactor), center.y + (velocity.y * slideFactor)); //限制最小[cornerRadius ]和最大边界值[self.view.bounds.size.width - cornerRadius],以免拖动出屏幕界限 finalPoint.x = MIN(MAX(finalPoint.x , cornerRadius), self.view.bounds.size.width - cornerRadius); finalPoint.y = MIN(MAX(finalPoint.y , cornerRadius), self.view.bounds.size.height - cornerRadius); //使用 UIView 动画使 view 滑行到终点
案例 import SwiftUI struct CustomViewModifier: ViewModifier { let color: Color let cornerRadius content .padding() .background(color) .clipShape(RoundedRectangle(cornerRadius : cornerRadius)) } } struct ContentView: View { var body: some View { VStack(spacing .frame(width: 100, height: 100) .modifier(CustomViewModifier(color: .red, cornerRadius : cornerRadius) .stroke(color, lineWidth: width) ) } // 添加卡片样式