做iOS开发的时候有可能经常碰到莫名其妙的滑动不顺畅(在UIScrollView以及其子类)或者在navigation切换的时候不顺畅的问题,这很有可能是在界面中加了太多带圆角并且masksToBounds为YES的UIView而引起的。
暂时所有版本的api都没有解决这个问题。因此如果想要避免这个问题,只能放弃在UIView上加圆角,如果该View是用来显示图片的,则可以将该UIView上的图片本身加圆角。
static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth,
float ovalHeight)
{
float fw, fh;
if (ovalWidth == 0 || ovalHeight == 0) {
CGContextAddRect(context, rect);
return;
}
CGContextSaveGState(context);
CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
CGContextScaleCTM(context, ovalWidth, ovalHeight);
fw = CGRectGetWidth(rect) / ovalWidth;
fh = CGRectGetHeight(rect) / ovalHeight;
CGContextMoveToPoint(context, fw, fh/2); // Start at lower right corner
CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); // Top right corner
CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // Top left corner
CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // Lower left corner
CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // Back to lower right
CGContextClosePath(context);
CGContextRestoreGState(context);
}
+ (id) createRoundedRectImage:(UIImage*)image size:(CGSize)size
{
// the size of CGContextRef
int w = size.width;
int h = size.height;
UIImage *img = image;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGRect rect = CGRectMake(0, 0, w, h);
CGContextBeginPath(context);
addRoundedRectToPath(context, rect, 10, 10);
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}
将UIView上的图片用该方法处理一次,就可以获得带了圆角的图片了。
分享到:
相关推荐
可以创建一个带有圆角和阴影的uiview
一行代码给任意视图添加高性能圆角,四个角可选 可添加描边属性
为UIView添加圆角扩展
基于UIView扩展的类目,用于设置上下左右圆角,可设置单边圆角,也可设置为圆形 github地址:https://github.com/LQQZYY/UIViewCornerDemo 喜欢的话就给颗星支持一下,感谢!
IOS圆角和阴影处理,以一张图片为例子 注释清晰,方便新手学习
使用拓展与runtime,为UIView添加手势事件
UIView 动画 UIView动画
UIView-BezierCurve 圆角化UIView的各个角
ios中关于uiview
主要介绍了iOS应用开发中UIView添加边框颜色及设置圆角边框的方法,代码基于传统的Objective-C,需要的朋友可以参考下
给 UIView 或 UIImage 高性能添加圆角,生成渐变色图片等
UIView AutoLayout ,UIView AutoLayout 是 UIView 的分类,实现了自动布局限制的简单接口。 T...
UIView+Extension 对view的一个扩展
JMRoundedCorner - UIView设置不触发离屏渲染的圆角
详细讲解UIView的生命循环周期,对开发者有更好的启发作用,深层次了解程序运行的流程。
1、将UIView 保存为图片,按照给定的scale 2、stackoverflow参照 3、保存的图片为依据scale,亲测
源码SkyRadiusView,实现view背景色的四个边角自定义成圆角 使用方法 使用方法很简单,只需要在xib或StoryBoard中继承SkyRadiusView,并设置参数即可 详情请参考项目中的示例代码 和 gif图片 测试环境:Xcode 6,...
IOS设置UIView的边框为圆角 iOS 系统自带的 View 组件都是正方形的,看起来都太生硬,有时候我需要变成圆角形式,如下图: 具体的实现是使用QuartzCore库,下面我具体的描述一下实现过程: • 首先创建一...
+ (UIView *_Nullable)az_gradientViewWithColors:(NSArray*> *_Nullable)colors locations:(NSArray*> *_Nullable)locations startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint; - (void)az_...
UIView-Positioning, 在UIView对象中,基于简单属性的框架属性设置 uiview定位收费 UIView Positioning 是一个快速扩展,它提供简单的shorthand 方法,以方便的方式定义任何UIView对象的框架属性( 宽度,高度,x,y ...