簡體   English   中英

UIButton陰影層效果

[英]UIButton shadow layer effect

我正在創建一個類似於這個圖像的UIButton: 在此輸入圖像描述

我通過使用以下代碼嘗試了它:

+(void)createShadowOnView:(UIView *)view color:(UIColor *)color width:(CGFloat)width height:(CGFloat)height shadowOpacity:(CGFloat)shadowOpacity andShadowRadius:(CGFloat)radius{

    view.layer.masksToBounds = NO;
    view.layer.shadowColor = color.CGColor;
    view.layer.shadowOffset = CGSizeMake(width,height);
    view.layer.shadowOpacity = shadowOpacity;
    [view.layer setShadowRadius:radius];
}

我能夠做到這一點:

在此輸入圖像描述

我希望Button上的陰影效果只保留在底部。

我怎樣才能達到預期的效果。

也許你應該設置視圖的backgroundcolor,所以標題沒有陰影,你可以設置view.layer.shadowOffset來改變陰影大小。

UIButton *customBTn = [UIButton buttonWithType:UIButtonTypeCustom];
customBTn.backgroundColor = [UIColor whiteColor];
customBTn.frame = CGRectMake(100, 100, 200, 50);
[customBTn setTitle:@"Sign Up" forState:UIControlStateNormal];
[customBTn setTitleColor:[UIColor colorWithRed:1/255.0 green:168/255.0 blue:244/255.0 alpha:1.0] forState:UIControlStateNormal];
customBTn.layer.borderColor = [UIColor colorWithRed:1/255.0 green:168/255.0 blue:244/255.0 alpha:1.0].CGColor;
customBTn.layer.borderWidth = 2;
customBTn.layer.cornerRadius = 25;
customBTn.layer.shadowColor = [UIColor lightGrayColor].CGColor;
customBTn.layer.shadowOffset = CGSizeMake(0,8);
customBTn.layer.shadowOpacity = 0.9;
[self.view addSubview:customBTn];

輸出: -

在此輸入圖像描述

只有您的代碼問題才會錯過為注冊按鈕設置背景顏色。 使用此代碼,

self.signUpButton.backgroundColor = [UIColor whiteColor];
[UIView createShadowOnView:self.signUpButton color:[UIColor lightGrayColor] width:0 height:2 shadowOpacity:0.3 andShadowRadius:0];

預習:

在此輸入圖像描述

碼:

UIButton *buttonWithShadow = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonWithShadow setFrame:CGRectMake(10.0f, 10.0f, 300.0f, 50.0f)];
[buttonWithShadow setTitle:@"SIGN UP" forState:UIControlStateNormal];
[buttonWithShadow setTitle:@"SIGN UP" forState:UIControlStateHighlighted];
[buttonWithShadow.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0f]];
[buttonWithShadow setTitleColor:[UIColor colorWithRed:1.0f/255.0 green:168.0f/255.0 blue:244.0f/255.0 alpha:1.0f] forState:UIControlStateNormal];
    [buttonWithShadow setBackgroundColor:[UIColor whiteColor]];
[buttonWithShadow.layer setBorderColor:[UIColor colorWithRed:1.0f/255.0 green:168.0f/255.0 blue:244.0f/255.0 alpha:1.0f].CGColor];
[buttonWithShadow.layer setBorderWidth:2.0f];
[buttonWithShadow.layer setCornerRadius:(buttonWithShadow.frame.size.height / 2.0f)];
[buttonWithShadow.layer setShadowColor:[UIColor lightGrayColor].CGColor];
[buttonWithShadow.layer setShadowOpacity:0.3f];
[buttonWithShadow.layer setShadowRadius:0.0f];
[buttonWithShadow.layer setShadowOffset:CGSizeMake(0.0f,2.0f)];
[buttonWithShadow.layer setMasksToBounds:NO];
[self.view addSubview:buttonWithShadow];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM