小編給大家分享一下IOS開發(fā)之swift中UIView擴(kuò)展使用的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)公司專注于招遠(yuǎn)企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站制作。招遠(yuǎn)網(wǎng)站建設(shè)公司,為招遠(yuǎn)等地區(qū)提供建站服務(wù)。全流程按需制作,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
IOS 開發(fā)之swift中UIView的擴(kuò)展使用的實(shí)例
擴(kuò)展類代碼:
import UIKit extension UIView { // MARK : 坐標(biāo)尺寸 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: - 位移 // 移動到指定中心點(diǎn)位置 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: - 邊框?qū)傩?nbsp; // 圓角邊框設(shè)置 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: - 翻轉(zhuǎn) // 旋轉(zhuǎn) 旋轉(zhuǎn)180度 M_PI func viewTransformWithRotation(rotation:CGFloat) -> Void { self.transform = CGAffineTransformMakeRotation(rotation); } // 縮放 func viewScaleWithSize(size:CGFloat) -> Void { self.transform = CGAffineTransformScale(self.transform, size, size); } // 水平,或垂直翻轉(zhuǎn) 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開發(fā)之swift中UIView擴(kuò)展使用的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!