如何在iOS中打起阴影?

时间:2018-09-20 19:10:52

标签: ios swift

我创建了一个圆形的UIView,它也应该有一个阴影。到目前为止,一切正常,但在拐角处,阴影未正确取整。

如何绕过阴影?

这是代码和屏幕截图:

popupView.layer.cornerRadius = 15

popupView.layer.shadowColor = UIColor.black.cgColor
popupView.layer.shadowOffset = CGSize(width: 0, height: 0)
popupView.layer.shadowOpacity = 0.3
popupView.layer.shadowRadius = 3.0
popupView.layer.shadowPath = UIBezierPath(rect: popupView.bounds).cgPath
popupView.layer.shouldRasterize = false

enter image description here

3 个答案:

答案 0 :(得分:3)

因此,我基本上将您的代码放入Playground中,并进行了一些尝试,发现我基本上可以删除view.layer.shadowPath,并且可以正常工作...

Basically works

import UIKit

let view = UIView(frame: CGRect(x: 100, y: 100, width: 500, height: 500))
view.backgroundColor = UIColor.white
view.layer.cornerRadius = 15

view.layer.shadowColor = UIColor.black.cgColor
view.layer.shadowOffset = CGSize(width: 0, height: 0)
view.layer.shadowOpacity = 1.0
view.layer.shadowRadius = 6.0
//view.layer.shadowPath = UIBezierPath(rect: view.bounds).cgPath
view.layer.shouldRasterize = false


let outter = UIView(frame: CGRect(x: 0, y: 0, width: 700, height: 700))
outter.backgroundColor = UIColor.red
outter.addSubview(view)


outter

我还发现是:

  • view.layer.masksToBounds = true未达到预期的结果
  • view.layer.shadowPath = UIBezierPath(roundedRect: view.bounds, cornerRadius: 15).cgPath也可以使用,但会增加一个故障点,因为如果您在视图上进行了更改,则需要记住设置cornerRadius属性,或者添加一个变量以同时为两者设定种子,但是由于如果您未设置shadowPath,它将产生相同的结果,这使我想知道为什么您都使用它。

答案 1 :(得分:2)

使用

 <div class="homepage-searchbar">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="align-middle">
                    <div class="col-md-3 col-sm-3">
                        <?php
                        $dataCategory=ArrayHelper::map(City::find()->all(),'id', 'city_name');

                        echo $form->field($searchModel, 'city_id')->widget(Select2::classname(), [
                            'data' => $dataCategory,
                            'theme' => Select2::THEME_BOOTSTRAP,
                            'options' => ['placeholder' => 'Select a city ...'],
                            'pluginOptions' => [
                                'allowClear' => true,
                            ],
                        ])->label(false); ?>
                    </div>
                    <div class="col-md-3 col-sm-3"> 
                        <?php echo \yii\helpers\Html::submitButton(Yii::t('frontend','Search Now'), ['class' => 'btn btn-default','onclick'=>'return searchRooms()']) ?>
                    </div>
                </div>
            </div>
        </div>    
    </div>
</div>

代替

popupView.layer.shadowPath = UIBezierPath(roundedRect: popupView.bounds, cornerRadius: 15).cgPath

并添加一点1.3的shadowOpacity以更好地看到阴影。

答案 2 :(得分:0)

您需要将视图图层masksToBounds值设置为true

popupView.layer.masksToBounds = true