自定义控件

Storyboard 自定义控件实现实时渲染

2016-04-12  本文已影响39人  一块豆腐

OC中

自定义控件的.h文件

#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface MyImageView : UIImageView

@property (nonatomic) IBInspectable CGFloat cornerRadius;

@end

自定义控件的.m文件

#import "MyImageView.h"

@implementation MyImageView

- (void)setCornerRadius:(CGFloat)cornerRadius
{
    _cornerRadius = cornerRadius;
    self.layer.cornerRadius = cornerRadius;
    self.layer.masksToBounds = cornerRadius > 0;
}

@end

Swift中

import UIKit

@IBDesignable
class MyImageView: UIImageView {

    @IBInspectable var cornerRadius: CGFloat = 0 {
        didSet {
            self.layer.cornerRadius = cornerRadius
            self.layer.masksToBounds = cornerRadius > 0
        }
    }
}

神奇的效果出现了

Snip20160413_1.png
上一篇下一篇

猜你喜欢

热点阅读