麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

如何在swift中實(shí)現(xiàn)oc中的分類

2019-11-14 17:58:10
字體:
供稿:網(wǎng)友

在oc中為了增強(qiáng)已有類的功能,我們經(jīng)常使用分類。使用分類,我們可以在不破壞原有類的結(jié)構(gòu)的前提下,對原有類進(jìn)行模塊化的擴(kuò)展。

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

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

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

那么我們怎么在swift中實(shí)現(xiàn)oc中的分類呢?我們可以向蘋果學(xué)習(xí)。
同樣是UIView類,我們分別看看oc和swift不同的實(shí)現(xiàn)方式。

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

其他的屬性和方法都是使用分類的方式進(jìn)行擴(kuò)展的。
下面是頁面渲染相關(guān)的屬性和方法

@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.創(chuàng)建我們自己的extension

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

//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中我們可以寫一個(gè)分類來實(shí)現(xiàn),這個(gè)難度不大。懶的自己寫的可以參照這個(gè)https://github.com/findM/UIView-Positioning
在swift中我們需要寫一個(gè)extension來實(shí)現(xiàn)
3.1 新建swift文件
新建swift文件.png
3.2 代碼實(shí)現(xiàn)

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        }    }    //其他的篇幅原因就不在這里一一實(shí)現(xiàn)了}

全部的實(shí)現(xiàn)方法請參考這里https://github.com/findM/UIView-Positioning-Swift


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 日韩精品一区二区三区中文 | 国产91精品亚洲精品日韩已满 | 国产精品久久久毛片 | 曰韩毛片| 国产精品久久久久久久久久大牛 | 久久精品国产久精国产 | 精品乱码久久久久 | 精品久久久久久国产 | 色戒在线版 | 国产精品久久久久久久亚洲按摩 | 成人免费一区 | 91中文在线观看 | 在线成人一区 | 欧美特黄a | 操操操日日日干干干 | 一级毛片特黄 | 国产精品剧情一区二区三区 | av不卡免费观看 | 在线 日本 制服 中文 欧美 | 视频国产一区二区 | 在线高清中文字幕 | 成av在线 | 国产毛片aaa一区二区三区视频 | 亚洲一二三久久 | 成人做爰高潮片免费视频韩国 | 成人情欲视频在线看免费 | 日韩视频―中文字幕 | 日韩大片在线永久观看视频网站免费 | 美女黄页网站免费进入 | 美国一级黄色毛片 | 99爱精品在线 | 一区二区三区日韩精品 | 国产精品久久久久免费视频 | 成人免费毛片一 | 香蕉久草视频 | 99热99精品 | 国产资源在线播放 | 欧美a在线观看 | 草莓福利社区在线 | 久久精品一区二区三区不卡牛牛 | 一级黄色毛片播放 |