亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

如何在swift中實現oc中的分類

2019-11-14 17:58:10
字體:
來源:轉載
供稿:網友

在oc中為了增強已有類的功能,我們經常使用分類。使用分類,我們可以在不破壞原有類的結構的前提下,對原有類進行模塊化的擴展。

但是在swift中沒有分類這種寫法了。相對應的是swift中只有擴展(Extensions)。

下面是swift中擴展(Extensions)的說明

擴展就是向一個已有的類、結構體、枚舉類型或者協議類型添加新功能(functionality)。這包括在沒有權限獲取原始源代碼的情況下擴展類型的能力(即逆向建模)。擴展和 Objective-C 中的分類(categories)類似。(不過與 Objective-C 不同的是,Swift 的擴展沒有名字。)

那么我們怎么在swift中實現oc中的分類呢?我們可以向蘋果學習。
同樣是UIView類,我們分別看看oc和swift不同的實現方式。

1.我們先看看oc中的UIView

這是 UIView中的聲明的代碼

#ifndef SDK_HIDE_TIDENS_CLASS_AVAILABLE_IOS(2_0) @interface UIView : UIResponder <NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem, UITraitEnvironment, UICoordinateSpace, UIFocusEnvironment>#elseNS_CLASS_AVAILABLE_IOS(2_0) @interface UIView : UIResponder <NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem, UITraitEnvironment, UICoordinateSpace>#endif+ (Class)layerClass;                        // default is [CALayer class]. Used when creating the underlying layer for the view.- (instancetype)initWithFrame:(CGRect)frame NS_DESIGNATED_INITIALIZER;- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;@PRoperty(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;  // default is YES. if set to NO, user events (touch, keys) are ignored and removed from the event queue.@property(nonatomic)                                 NSInteger tag;                // default is 0@property(nonatomic,readonly,strong)                 CALayer  *layer;              // returns view's layer. Will always return a non-nil value. view is layer's delegate#ifndef SDK_HIDE_TIDE- (BOOL)canBecomeFocused NS_AVAILABLE_IOS(9_0); // NO by default@property (readonly, nonatomic, getter=isFocused) BOOL focused NS_AVAILABLE_IOS(9_0);#endif+ (UIUserInterfaceLayoutDirection)userInterfaceLayoutDirectionForSemanticContentAttribute:(UISemanticContentAttribute)attribute NS_AVAILABLE_IOS(9_0);@property (nonatomic) UISemanticContentAttribute semanticContentAttribute NS_AVAILABLE_IOS(9_0);@end

其他的屬性和方法都是使用分類的方式進行擴展的。
下面是頁面渲染相關的屬性和方法

@interface UIView(UIViewRendering)- (void)drawRect:(CGRect)rect;- (void)setNeedsDisplay;- (void)setNeedsDisplayInRect:(CGRect)rect;@property(nonatomic)                 BOOL              clipsToBounds;              // When YES, content and subviews are clipped to the bounds of the view. Default is NO.@property(nullable, nonatomic,copy)            UIColor          *backgroundColor UI_APPEARANCE_SELECTOR; // default is nil. Can be useful with the appearance proxy on custom UIView subclasses.@property(nonatomic)                 CGFloat           alpha;                      // animatable. default is 1.0@property(nonatomic,getter=isOpaque) BOOL              opaque;                     // default is YES. opaque views must fill their entire bounds or the results are undefined. the active CGContext in drawRect: will not have been cleared and may have non-zeroed pixels@property(nonatomic)                 BOOL              clearsContextBeforeDrawing; // default is YES. ignored for opaque views. for non-opaque views causes the active CGContext in drawRect: to be pre-filled with transparent pixels@property(nonatomic,getter=isHidden) BOOL              hidden;                     // default is NO. doesn't check superviews@property(nonatomic)                 UIViewContentMode contentMode;                // default is UIViewContentModeScaleToFill@property(nonatomic)                 CGRect            contentStretch NS_DEPRECATED_IOS(3_0,6_0) __TVOS_PROHIBITED; // animatable. default is unit rectangle {{0,0} {1,1}}. Now deprecated: please use -[UIImage resizableImageWithCapInsets:] to achieve the same effect.@property(nullable, nonatomic,strong)          UIView           *maskView NS_AVAILABLE_IOS(8_0);/* -tintColor always returns a color. The color returned is the first non-default value in the receiver's superview chain (starting with itself). If no non-default value is found, a system-defined color is returned. If this view's -tintAdjustmentMode returns Dimmed, then the color that is returned for -tintColor will automatically be dimmed. If your view subclass uses tintColor in its rendering, override -tintColorDidChange in order to refresh the rendering if the color changes. */@property(null_resettable, nonatomic, strong) UIColor *tintColor NS_AVAILABLE_IOS(7_0);/* -tintAdjustmentMode always returns either UIViewTintAdjustmentModeNormal or UIViewTintAdjustmentModeDimmed. The value returned is the first non-default value in the receiver's superview chain (starting with itself). If no non-default value is found, UIViewTintAdjustmentModeNormal is returned. When tintAdjustmentMode has a value of UIViewTintAdjustmentModeDimmed for a view, the color it returns from tintColor will be modified to give a dimmed appearance. When the tintAdjustmentMode of a view changes (either the view's value changing or by one of its superview's values changing), -tintColorDidChange will be called to allow the view to refresh its rendering. */@property(nonatomic) UIViewTintAdjustmentMode tintAdjustmentMode NS_AVAILABLE_IOS(7_0);/* The -tintColorDidChange message is sent to appropriate subviews of a view when its tintColor is changed by client code or to subviews in the view hierarchy of a view whose tintColor is implicitly changed when its superview or tintAdjustmentMode changes. */- (void)tintColorDidChange NS_AVAILABLE_IOS(7_0);@end

2.這是swift中的代碼

UIView類中的聲明

@available(iOS 2.0, *)    public class UIView : UIResponder, NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem, UITraitEnvironment, UICoordinateSpace, UIFocusEnvironment {        public class func layerClass() -> AnyClass // default is [CALayer class]. Used when creating the underlying layer for the view.        public init(frame: CGRect)    public init?(coder aDecoder: NSCoder)        public var userInteractionEnabled: Bool // default is YES. if set to NO, user events (touch, keys) are ignored and removed from the event queue.    public var tag: Int // default is 0    public var layer: CALayer { get } // returns view's layer. Will always return a non-nil value. view is layer's delegate        @available(iOS 9.0, *)    public func canBecomeFocused() -> Bool // NO by default    @available(iOS 9.0, *)    public var focused: Bool { get }        @available(iOS 9.0, *)    public class func userInterfaceLayoutDirectionForSemanticContentAttribute(attribute: UISemanticContentAttribute) -> UIUserInterfaceLayoutDirection    @available(iOS 9.0, *)    public var semanticContentAttribute: UISemanticContentAttribute}

頁面渲染的代碼

extension UIView {        public func drawRect(rect: CGRect)        public func setNeedsDisplay()    public func setNeedsDisplayInRect(rect: CGRect)        public var clipsToBounds: Bool // When YES, content and subviews are clipped to the bounds of the view. Default is NO.    @NSCopying public var backgroundColor: UIColor? // default is nil. Can be useful with the appearance proxy on custom UIView subclasses.    public var alpha: CGFloat // animatable. default is 1.0    public var opaque: Bool // default is YES. opaque views must fill their entire bounds or the results are undefined. the active CGContext in drawRect: will not have been cleared and may have non-zeroed pixels    public var clearsContextBeforeDrawing: Bool // default is YES. ignored for opaque views. for non-opaque views causes the active CGContext in drawRect: to be pre-filled with transparent pixels    public var hidden: Bool // default is NO. doesn't check superviews    public var contentMode: UIViewContentMode // default is UIViewContentModeScaleToFill    // animatable. default is unit rectangle {{0,0} {1,1}}. Now deprecated: please use -[UIImage resizableImageWithCapInsets:] to achieve the same effect.        @available(iOS 8.0, *)    public var maskView: UIView?        /*     -tintColor always returns a color. The color returned is the first non-default value in the receiver's superview chain (starting with itself).     If no non-default value is found, a system-defined color is returned.     If this view's -tintAdjustmentMode returns Dimmed, then the color that is returned for -tintColor will automatically be dimmed.     If your view subclass uses tintColor in its rendering, override -tintColorDidChange in order to refresh the rendering if the color changes.     */    @available(iOS 7.0, *)    public var tintColor: UIColor!        /*     -tintAdjustmentMode always returns either UIViewTintAdjustmentModeNormal or UIViewTintAdjustmentModeDimmed. The value returned is the first non-default value in the receiver's superview chain (starting with itself).     If no non-default value is found, UIViewTintAdjustmentModeNormal is returned.     When tintAdjustmentMode has a value of UIViewTintAdjustmentModeDimmed for a view, the color it returns from tintColor will be modified to give a dimmed appearance.     When the tintAdjustmentMode of a view changes (either the view's value changing or by one of its superview's values changing), -tintColorDidChange will be called to allow the view to refresh its rendering.     */    @available(iOS 7.0, *)    public var tintAdjustmentMode: UIViewTintAdjustmentMode        /*     The -tintColorDidChange message is sent to appropriate subviews of a view when its tintColor is changed by client code or to subviews in the view hierarchy of a view whose tintColor is implicitly changed when its superview or tintAdjustmentMode changes.     */    @available(iOS 7.0, *)    public func tintColorDidChange()}

3.創建我們自己的extension

下面就讓我們嘗試一下,寫一個自己的swift分類
在get和set UIView的x和y屬性的時候代碼很繁瑣,如下:

//get xvar x = self.view.frame.origin.x//set xvar rect = self.view.framerect.origin.x = 100self.view.frame = rect

我們希望這里的代碼是這樣的

//get xvar x = self.view.x//set xself.view.x = 100

在oc中我們可以寫一個分類來實現,這個難度不大。懶的自己寫的可以參照這個https://github.com/findM/UIView-Positioning
在swift中我們需要寫一個extension來實現
3.1 新建swift文件
新建swift文件.png
3.2 代碼實現

import Foundationimport UIKit//private var PERSON_ID_NUMBER_PROPERTY = 0extension UIView {    public var x: CGFloat{        get{            return self.frame.origin.x        }        set{            var r = self.frame            r.origin.x = newValue            self.frame = r        }    }    public var y: CGFloat{        get{            return self.frame.origin.y        }        set{            var r = self.frame            r.origin.y = newValue            self.frame = r        }    }    //其他的篇幅原因就不在這里一一實現了}

全部的實現方法請參考這里https://github.com/findM/UIView-Positioning-Swift


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美刺激性大交免费视频| 欧美大片va欧美在线播放| 亚州av一区二区| 成人激情黄色网| 97香蕉超级碰碰久久免费软件| 亚洲xxxxx| 成人午夜高潮视频| 亚洲欧美日韩另类| 国产欧美日韩视频| 国产精品老女人视频| 亚洲欧美中文日韩在线| 狠狠色香婷婷久久亚洲精品| 欧美成人手机在线| 国产欧美日韩精品在线观看| 精品日本高清在线播放| 亚洲欧美一区二区精品久久久| 国产区亚洲区欧美区| 日韩欧美精品中文字幕| 欧美成人在线免费视频| 91精品国产乱码久久久久久蜜臀| 亚洲欧洲xxxx| 国产欧美在线观看| 成人免费福利视频| 另类天堂视频在线观看| 久久好看免费视频| 精品欧美国产一区二区三区| 国产亚洲一区精品| 91香蕉嫩草影院入口| 色偷偷偷综合中文字幕;dd| 国产原创欧美精品| 日韩电影视频免费| 97色在线播放视频| 日韩在线中文字| 日韩精品中文字幕视频在线| 国产在线日韩在线| 欧美日韩激情视频| 91精品啪在线观看麻豆免费| 不卡av电影院| 91在线免费看网站| 欧美肥婆姓交大片| 国产精品999| 中文字幕欧美国内| 2018国产精品视频| 亚洲视频在线免费观看| 日韩不卡中文字幕| 国产乱人伦真实精品视频| 亚洲成人激情在线观看| 成人动漫网站在线观看| 91精品国产一区| 国产999在线观看| 欧美午夜视频在线观看| 国产欧美亚洲精品| 久久九九亚洲综合| 亚洲xxxx在线| 日本不卡高字幕在线2019| 成人网中文字幕| 国产女精品视频网站免费| 国产精品成人久久久久| 欧美激情综合亚洲一二区| 国产欧美亚洲精品| 欧美亚洲一区在线| 九九热这里只有在线精品视| 国产精品视频资源| 国产在线观看91精品一区| 国产一区二区三区日韩欧美| 欧洲精品久久久| 国产精品香蕉国产| 国产精品伦子伦免费视频| 久久69精品久久久久久国产越南| 日韩欧美亚洲成人| 精品久久久免费| 中文字幕国产精品久久| 97视频在线看| 欧美日韩在线看| 久久99久久亚洲国产| 欧美日韩国产999| 伊人久久久久久久久久| 国产精品专区第二| 欧美日韩性视频| 国产女精品视频网站免费| 日韩日本欧美亚洲| 一区二区三区视频观看| 久久人人爽亚洲精品天堂| 国产精品女人久久久久久| 8090成年在线看片午夜| 国产成+人+综合+亚洲欧美丁香花| 国产精品久久久久99| 亚洲高清一二三区| 欧美激情视频播放| 亚洲欧美视频在线| 欧美成人午夜激情视频| 国产欧美久久久久久| 亚洲人成网站999久久久综合| 久久九九全国免费精品观看| 亚洲理论在线a中文字幕| 成人淫片在线看| 国产国语videosex另类| 国产精品视频在线播放| 国产91色在线| 欧美激情在线狂野欧美精品| 在线电影中文日韩| 亚洲欧美中文日韩在线v日本| 黑人巨大精品欧美一区免费视频| 主播福利视频一区| 亚洲大胆人体av| 亚洲美女性视频| 色狠狠久久aa北条麻妃| 大胆人体色综合| 国产成人av在线| 欧美性猛交xxxx偷拍洗澡| 一区二区三区在线播放欧美| 亚洲精品国产精品自产a区红杏吧| 中文字幕在线视频日韩| 黑人狂躁日本妞一区二区三区| 久久久97精品| 久久免费视频网站| 欧美视频第一页| 亚洲成人999| 中文字幕一区日韩电影| 亚洲电影av在线| 不卡毛片在线看| 欧美电影免费观看网站| 精品久久香蕉国产线看观看亚洲| 精品少妇一区二区30p| 国产精品午夜国产小视频| 欧美大肥婆大肥bbbbb| 欧美性猛交xxxx久久久| 丰满岳妇乱一区二区三区| 隔壁老王国产在线精品| 欧美丝袜美女中出在线| 国产网站欧美日韩免费精品在线观看| 欧美大片在线看| 91在线直播亚洲| 欧美疯狂做受xxxx高潮| 欧美裸体男粗大视频在线观看| 国产精品一区二区三区久久久| 久久久久www| 九九热在线精品视频| 久久精品国产免费观看| 日韩成人激情视频| 亚洲精品久久久久久久久| 日本sm极度另类视频| 国产日韩欧美一二三区| 最新的欧美黄色| 日韩精品丝袜在线| 欧美激情视频给我| 国产精品极品在线| 2020欧美日韩在线视频| 久久视频免费观看| 亚洲成成品网站| 欧美午夜片在线免费观看| 91精品久久久久久久| 亚洲美女动态图120秒| 国产日韩欧美综合| 欧美www在线| 亚洲精品动漫100p| 日韩久久免费视频| 亚洲新中文字幕| 亚洲国产精品中文| 欧美日韩美女在线观看| 精品国产户外野外| 永久免费毛片在线播放不卡| 亚洲精品99久久久久| 精品国产精品自拍|