簡體   English   中英

iOS:如何在UIView上添加陰影和筆觸陰影?

[英]iOS : How to add drop shadow and stroke shadow on UIView?

我想在UIView上添加陰影筆觸陰影這是我的設計師給我的應用陰影,

  • 對於投影,他告訴我使用RGB(176,199,226),不透明度為90%,距離為3,尺寸為5

  • 對於描邊陰影,他告訴應用RGB(209,217,226)大小為1和100%不透明度。

這是photoshop應用效果屏幕,

在此輸入圖像描述在此輸入圖像描述

具有所需陰影的視圖(預期輸出)

在此輸入圖像描述

我嘗試了以下方法來獲得投影

viewCheck.layer.masksToBounds = NO;
viewCheck.layer.cornerRadius = 5.f;
viewCheck.layer.shadowOffset = CGSizeMake(.0f,2.5f);
viewCheck.layer.shadowRadius = 1.5f;
viewCheck.layer.shadowOpacity = .9f;
viewCheck.layer.shadowColor = [UIColor colorWithRed:176.f/255.f green:199.f/255.f blue:226.f/255.f alpha:1.f].CGColor;
viewCheck.layer.shadowPath = [UIBezierPath bezierPathWithRect:viewCheck.bounds].CGPath;

這就是它的結果,

在此輸入圖像描述

我在理解如何將中風和陰影應用到photoshop截圖中時遇到問題(我已在上面添加)。 如何應用距離,點差,大小,位置?

有人能指出我應用這些陰影的指南嗎? 有很多陰影效果出現,我想學習如何在這里提出每個問題!

謝謝 :)

我相信你尋找的大部分配置都可以通過使用shadowPath屬性來實現。

viewCheck.layer.shadowRadius  = 1.5f;
viewCheck.layer.shadowColor   = [UIColor colorWithRed:176.f/255.f green:199.f/255.f blue:226.f/255.f alpha:1.f].CGColor;
viewCheck.layer.shadowOffset  = CGSizeMake(0.0f, 0.0f);
viewCheck.layer.shadowOpacity = 0.9f;
viewCheck.layer.masksToBounds = NO;

UIEdgeInsets shadowInsets     = UIEdgeInsetsMake(0, 0, -1.5f, 0);
UIBezierPath *shadowPath      = [UIBezierPath bezierPathWithRect:UIEdgeInsetsInsetRect(viewCheck.bounds, shadowInsets)];
viewCheck.layer.shadowPath    = shadowPath.CGPath;

例如,通過這種方式設置shadowInsets

UIEdgeInsets shadowInsets = UIEdgeInsetsMake(0, 0, -1.5f, 0);

你會得到一個很好的理想陰影:

在此輸入圖像描述

您現在可以通過控制陰影路徑矩形插圖來決定如何構建陰影矩形。

以下是User Defined Runtime Attribute的步驟。

  1. 在Interface Builder中打開storyboard或xib文件。
  2. 在Interface Builder中,選擇要添加屬性的對象。
  3. 選擇“視圖”>“實用程序”>“顯示標識檢查器”。

身份檢查器出現在實用程序區域中。 如下所示,用戶定義的運行時屬性編輯器是檢查器中的項目之一。

在此輸入圖像描述

  1. 單擊用戶定義的運行時屬性編輯器左下角的“添加”按鈕(+)。

在此輸入圖像描述

為了給邊界UIView

添加#import "QuartzCore/QuartzCore.h"框架工作。

myView.layer.cornerRadius = 15.0; // set as you want.
myView.layer.borderColor = [UIColor lightGrayColor].CGColor; // set color as you want.
myView.layer.borderWidth = 1.0; // set as you want.

對不起,我只有Swift解決方案但是我使用UIView擴展來執行此任務:

// MARK: Layer Extensions
extension UIView {

    func setCornerRadius(#radius: CGFloat) {
        self.layer.cornerRadius = radius
        self.layer.masksToBounds = true
    }

    func addShadow(#offset: CGSize, radius: CGFloat, color: UIColor, opacity: Float, cornerRadius: CGFloat? = nil) {
        self.layer.shadowOffset = offset
        self.layer.shadowRadius = radius
        self.layer.shadowOpacity = opacity
        self.layer.shadowColor = color.CGColor
        if let r = cornerRadius {
            self.layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: r).CGPath
        }
    }

    func addBorder(#width: CGFloat, color: UIColor) {
        self.layer.borderWidth = width
        self.layer.borderColor = color.CGColor
        self.layer.masksToBounds = true
    }

    func drawStroke(#width: CGFloat, color: UIColor) {
        let path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: self.w, height: self.w), cornerRadius: self.w/2)
        let shapeLayer = CAShapeLayer ()
        shapeLayer.path = path.CGPath
        shapeLayer.fillColor = UIColor.clearColor().CGColor
        shapeLayer.strokeColor = color.CGColor
        shapeLayer.lineWidth = width
        self.layer.addSublayer(shapeLayer)
    }

}

我從未嘗試過,但您可以將此代碼粘貼到任何Swift文件中,並可能從Obj-C代碼中調用它們。

暫無
暫無

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

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