菜鸟教程小白 发表于 2022-12-12 20:04:34

ios - 试图创建一个圆角 UIButton 类


                                            <p><p>我正在尝试创建一个圆角 UIButton 类,但它不起作用。这是我目前所拥有的:</p>

<p><strong>界面</strong></p>

<pre><code>#import &lt;UIKit/UIKit.h&gt;

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
</code></pre>

<p><strong>实现</strong></p>

<pre><code>#import &#34;BotaoCantosArredondados.h&#34;
@import QuartzCore;


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


@implementation BotaoCantosArredondados

- (void)drawRect:(CGRect)rect {

;

if (sombraLayer) {
    ;
}

sombraLayer = ;
sombraLayer.backgroundColor = [ CGColor];
sombraLayer.shadowColor = ;
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;

;

}

- (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;
}
</code></pre>

<p>有什么想法吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您应该尝试操作按钮的层,即;</p>

<pre><code>#import &lt;UIKit/UIKit.h&gt;

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
</code></pre>

<p>和实现;</p>

<pre><code>#import &#34;BotaoCantosArredondados.h&#34;

@implementation TestButton

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

    }
    return self; }


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

}

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

}

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

}

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

}

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

@end
</code></pre>

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

<p>检查一下,</p>

<p> <a href="/image/psO97.gif" rel="noreferrer noopener nofollow"><img src="/image/psO97.gif" alt="enter image description here"/></a> </p>

<p>输出:-</p>

<p> <a href="/image/IkJd2.png" rel="noreferrer noopener nofollow"><img src="/image/IkJd2.png" alt="enter image description here"/></a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 试图创建一个圆角 UIButton 类,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37271752/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37271752/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 试图创建一个圆角 UIButton 类