您好,登錄后才能下訂單哦!
小編給大家分享一下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擴展使用的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。