首页 > 系统 > iOS > 正文

IOS 开发之swift中UIView的扩展使用的实例

2020-07-26 02:41:44
字体:
来源:转载
供稿:网友

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) 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本的支持!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表