尝试创建圆角UIButton类

时间:2016-05-17 09:07:55

标签: ios iphone uibutton ibdesignable ibinspectable

我正在尝试创建一个圆角UIButton类,但它无法正常工作。这就是我到目前为止所做的:

接口

#import <UIKit/UIKit.h>

IB_DESIGNABLE

@interface BotaoCantosArredondados : UIButton

@property (nonatomic, assign) IBInspectable CGFloat radius;
@property (nonatomic, assign) IBInspectable CGSize shadowOffset;
@property (nonatomic, assign) IBInspectable CGFloat shadowRadius;
@property (nonatomic, assign) IBInspectable CGFloat shadowOpacity;
@property (nonatomic, strong) IBInspectable UIColor *shadowColor;


@end

实施

#import "BotaoCantosArredondados.h"
@import QuartzCore;


@interface BotaoCantosArredondados() {
  CALayer *sombraLayer;
}
@end


@implementation BotaoCantosArredondados

- (void)drawRect:(CGRect)rect {

  [super drawRect:rect];

  if (sombraLayer) {
    [sombraLayer removeFromSuperlayer];
  }

  sombraLayer = [CALayer layer];
  sombraLayer.backgroundColor = [[UIColor clearColor] CGColor];
  sombraLayer.shadowColor = [_shadowColor CGColor];
  UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                                  cornerRadius:_radius];
  path.lineWidth = _shadowRadius;
  sombraLayer.shadowPath = path.CGPath;

  sombraLayer.shadowOffset = _shadowOffset;
  sombraLayer.shadowOpacity = _shadowOpacity;
  sombraLayer.shadowRadius = _radius;
  sombraLayer.masksToBounds = YES;

  [self.layer addSublayer:sombraLayer];

}

- (void)setShadowColor:(UIColor *)shadowColor {
  _shadowColor = shadowColor;
}

- (void)setShadowOffset:(CGSize)shadowOffset {
  _shadowOffset = shadowOffset;
}

- (void)setShadowRadius:(CGFloat)shadowRadius {
  _shadowRadius = shadowRadius;
}

- (void)setShadowOpacity:(CGFloat)shadowOpacity {
  _shadowOpacity = shadowOpacity;
}

- (void)setRadius:(CGFloat)radius {
  _radius = radius;
}

任何想法?

1 个答案:

答案 0 :(得分:2)

你应该尝试操纵Button的层,即

#import <UIKit/UIKit.h>

IB_DESIGNABLE

@interface BotaoCantosArredondados : UIButton

@property (nonatomic, assign) IBInspectable CGFloat radius;
@property (nonatomic, assign) IBInspectable CGSize shadowOffset;
@property (nonatomic, assign) IBInspectable CGFloat shadowRadius;
@property (nonatomic, assign) IBInspectable CGFloat shadowOpacity;
@property (nonatomic, strong) IBInspectable UIColor *shadowColor;

@end

和实施;

#import "BotaoCantosArredondados.h"

@implementation TestButton

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.clipsToBounds = NO;

    }
    return self; }


- (void)setShadowColor:(UIColor *)shadowColor {
    _shadowColor = shadowColor;
    self.layer.shadowColor = shadowColor.CGColor;
    [self.layer setNeedsDisplay];

}

- (void)setShadowOffset:(CGSize)shadowOffset {
    _shadowOffset = shadowOffset;
    self.layer.shadowOffset = shadowOffset;
    [self.layer setNeedsDisplay];

}

- (void)setShadowRadius:(CGFloat)shadowRadius {
    _shadowRadius = shadowRadius;
    self.layer.shadowRadius = shadowRadius;
    [self.layer setNeedsDisplay];

}

- (void)setShadowOpacity:(CGFloat)shadowOpacity {
    _shadowOpacity = shadowOpacity;
    self.layer.shadowOpacity = shadowOpacity;
    [self.layer setNeedsDisplay];

}

- (void)setRadius:(CGFloat)radius {
    _radius = radius;
    self.layer.cornerRadius = radius;
    [self.layer setNeedsDisplay]; }

@end

编辑: - 您需要删除assign子类,然后手动删除所有属性。然后再次将类分配给按钮,然后再运行程序。

检查一下,

enter image description here

输出: -

enter image description here