欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-18 15:26:16
ios 开发之swift中uiview的扩展使用的实例 扩展类代码: import uikit extension 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); 
    } 
  } 
} 

使用效果图:

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

使用示例:

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) 

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

上一篇:

下一篇: