UIButton에 그림자를 추가하는 방법은 무엇입니까?
UIButton에 그림자를 추가하고 싶습니다. self.layer.shadow * 속성을 사용하려고했습니다. 이러한 속성은 UIView에서 작동하지만 UIButton에서는 다르게 작동합니다. 그림자를 그릴 수있는 포인터를 얻을 수 있다면 정말 감사하겠습니다. 감사합니다!
self.layer.cornerRadius = 8.0f;
self.layer.masksToBounds = YES;
self.layer.borderWidth = 1.0f;
self.layer.shadowColor = [UIColor greenColor].CGColor;
self.layer.shadowOpacity = 0.8;
self.layer.shadowRadius = 12;
self.layer.shadowOffset = CGSizeMake(12.0f, 12.0f);
그림자가 표시되지 않도록하는 질문에는 단 한 가지 작은 실수가 있습니다. masksToBounds:YES또한 그림자를가립니다! 올바른 코드는 다음과 같습니다.
self.layer.cornerRadius = 8.0f;
self.layer.masksToBounds = NO;
self.layer.borderWidth = 1.0f;
self.layer.shadowColor = [UIColor greenColor].CGColor;
self.layer.shadowOpacity = 0.8;
self.layer.shadowRadius = 12;
self.layer.shadowOffset = CGSizeMake(12.0f, 12.0f);
불행히도 이것은 우리가 속임수 없이는 콘텐츠를 가리고 동시에 그림자를 가질 수 없다는 것을 의미합니다.
을 기억하십시오 #import <QuartzCore/QuartzCore.h>.
다음을 사용하는 경우 쉬운 해결책은 다음과 같습니다 UIButton.
#import <QuartzCore/QuartzCore.h>
button.imageView.layer.cornerRadius = 7.0f;
button.layer.shadowRadius = 3.0f;
button.layer.shadowColor = [UIColor blackColor].CGColor;
button.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
button.layer.shadowOpacity = 0.5f;
button.layer.masksToBounds = NO;
제대로 작동하고 게시 할 가치가 있다고 생각했습니다. 도움이 되길 바랍니다!
여기에 그림자를 만들뿐만 아니라 버튼을 누르면 아래로 움직이는 하위 클래스가 있습니다.
//
// ShadowButton.h
#import <Foundation/Foundation.h>
@interface ShadowButton : UIButton {
}
@end
//
// ShadowButton.m
#import "ShadowButton.h"
#import <QuartzCore/QuartzCore.h>
@implementation ShadowButton
-(void)setupView{
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOpacity = 0.5;
self.layer.shadowRadius = 1;
self.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
}
-(id)initWithFrame:(CGRect)frame{
if((self = [super initWithFrame:frame])){
[self setupView];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder{
if((self = [super initWithCoder:aDecoder])){
[self setupView];
}
return self;
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
self.contentEdgeInsets = UIEdgeInsetsMake(1.0,1.0,-1.0,-1.0);
self.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
self.layer.shadowOpacity = 0.8;
[super touchesBegan:touches withEvent:event];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
self.contentEdgeInsets = UIEdgeInsetsMake(0.0,0.0,0.0,0.0);
self.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
self.layer.shadowOpacity = 0.5;
[super touchesEnded:touches withEvent:event];
}
@end
You can create a subclass and configure its values in Xcode
Below is an example:
import UIKit
import QuartzCore
@IBDesignable
class CustomButton: UIButton {
@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadius
}
}
@IBInspectable var borderWidth: CGFloat = 0 {
didSet {
layer.borderWidth = borderWidth
}
}
@IBInspectable var borderColor: UIColor = UIColor.gray {
didSet {
layer.borderColor = borderColor.cgColor
}
}
@IBInspectable var shadowColor: UIColor = UIColor.gray {
didSet {
layer.shadowColor = shadowColor.cgColor
}
}
@IBInspectable var shadowOpacity: Float = 1.0 {
didSet {
layer.shadowOpacity = shadowOpacity
}
}
@IBInspectable var shadowRadius: CGFloat = 1.0 {
didSet {
layer.shadowRadius = shadowRadius
}
}
@IBInspectable var masksToBounds: Bool = true {
didSet {
layer.masksToBounds = masksToBounds
}
}
@IBInspectable var shadowOffset: CGSize = CGSize(width: 12, height: 12) {
didSet {
layer.shadowOffset = shadowOffset
}
}
}
You can subclass UIButton and overwrite the drawRect: method and add manually a drop shadow. That's much more work and you should now a few things about quartz 2d, but the result is exactly what you want. Otherwise you could just add an image, but I prefere to subclass UIButton, because it is very flexible regarding the size of the button, it's more general.
참고URL : https://stackoverflow.com/questions/2315366/how-to-add-a-drop-shadow-to-a-uibutton
'program story' 카테고리의 다른 글
| Neo4J는 ID로 노드 가져 오기 (0) | 2020.10.19 |
|---|---|
| 창 핸들이 만들어 질 때까지 컨트롤에서 Invoke 또는 BeginInvoke를 호출 할 수 없습니다. (0) | 2020.10.19 |
| Python + Selenium WebDriver를 사용하여 쿠키를 저장하고로드하는 방법 (0) | 2020.10.18 |
| Emacs 버퍼에서 단어의 모든 발생을 강조하는 방법은 무엇입니까? (0) | 2020.10.18 |
| XElement 또는 LINQ에서 XPath를 사용하는 방법은 무엇입니까? (0) | 2020.10.18 |