IOS开发之swift中UIView扩展使用的示例分析
小编给大家分享一下IOS开发之swift中UIView扩展使用的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
创新互联公司专注于招远企业网站建设,成都响应式网站建设公司,商城网站制作。招远网站建设公司,为招远等地区提供建站服务。全流程按需制作,专业设计,全程项目跟踪,创新互联公司专业和态度为您提供的服务
IOS 开发之swift中UIView的扩展使用的实例
扩展类代码:
import UIKit extension UIView { // MARK : 坐标尺寸 var origin:CGPoint { get { return self.frame.origin } set(newValue) { var rect = self.frame rect.origin = newValue self.frame = rect } } var size:CGSize { get { return self.frame.size } set(newValue) { var rect = self.frame rect.size = newValue self.frame = rect } } var left:CGFloat { get { return self.frame.origin.x } set(newValue) { var rect = self.frame rect.origin.x = newValue self.frame = rect } } var top:CGFloat { get { return self.frame.origin.y } set(newValue) { var rect = self.frame rect.origin.y = newValue self.frame = rect } } var right:CGFloat { get { return (self.frame.origin.x + self.frame.size.width) } set(newValue) { var rect = self.frame rect.origin.x = (newValue - self.frame.size.width) self.frame = rect } } var bottom:CGFloat { get { return (self.frame.origin.y + self.frame.size.height) } set(newValue) { var rect = self.frame rect.origin.y = (newValue - self.frame.size.height) self.frame = rect } } // MARK: - 位移 // 移动到指定中心点位置 func moveToPoint(point:CGPoint) -> Void { var center = self.center center.x = point.x center.y = point.y self.center = center } // 缩放到指定大小 func scaleToSize(scale:CGFloat) -> Void { var rect = self.frame rect.size.width *= scale rect.size.height *= scale self.frame = rect } // MARK: - 毛玻璃效果 // 毛玻璃 func effectViewWithAlpha(alpha:CGFloat) -> Void { let effect = UIBlurEffect.init(style: UIBlurEffectStyle.Light) let effectView = UIVisualEffectView.init(effect: effect) effectView.frame = self.bounds effectView.alpha = alpha self.addSubview(effectView) } // MARK: - 边框属性 // 圆角边框设置 func layer(radius radius:CGFloat, borderWidth:CGFloat, borderColor:UIColor) -> Void { if (0.0 < radius) { self.layer.cornerRadius = radius self.layer.masksToBounds = true self.clipsToBounds = true } if (0.0 < borderWidth) { self.layer.borderColor = borderColor.CGColor self.layer.borderWidth = borderWidth } } // MARK: - 翻转 // 旋转 旋转180度 M_PI func viewTransformWithRotation(rotation:CGFloat) -> Void { self.transform = CGAffineTransformMakeRotation(rotation); } // 缩放 func viewScaleWithSize(size:CGFloat) -> Void { self.transform = CGAffineTransformScale(self.transform, size, size); } // 水平,或垂直翻转 func viewFlip(isHorizontal:Bool) -> Void { if (isHorizontal) { // 水平 self.transform = CGAffineTransformScale(self.transform, -1.0, 1.0); } else { // 垂直 self.transform = CGAffineTransformScale(self.transform, 1.0, -1.0); } } }
使用效果图:
使用示例:
let imageView = UIImageView.init(frame: CGRectMake(10.0, 10.0, 60.0, 60.0)) self.view.addSubview(imageView) imageView.image = UIImage(named: "header") imageView.backgroundColor = UIColor.colorRandom() imageView.left = 100.0 imageView.top = 100.0 imageView.size = CGSizeMake(100.0, 100.0) imageView.moveToPoint(CGPointMake(100.0, 100.0)) imageView.scaleToSize(2.0) imageView.effectViewWithAlpha(0.6) imageView.layer(radius: 3.0, borderWidth: 1.0, borderColor: UIColor.redColor()) imageView.viewFlip(false) imageView.viewTransformWithRotation(5.0)
以上是“IOS开发之swift中UIView扩展使用的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!
网页名称:IOS开发之swift中UIView扩展使用的示例分析
当前地址:http://cdiso.cn/article/ieoiod.html