0

背景として画像があるストーリー ボード シーンに透明な削除UIButtonを追加し、いくつかの自動レイアウト制約を追加しました。

ボタンの背景を にしたいので、ボタンUIVisualEffectViewがある場所にプログラムでビューを追加し、ボタンを削除して追加し直して、UIVisualEffectView. 質問 1) これは良い考えですか? または、ビュー階層内の位置を見つけて、ボタンの 1 レベル前に配置する必要がありますか?

これが私がこれまでに持っているものです。のためにUIButton

@IBOutlet weak var delete: UIButton! {
    didSet {
        let borderAlpha = CGFloat(0.7)
        let cornerRadius = CGFloat(5.0)
        delete.setTitle("Delete", forState: UIControlState.Normal)
        delete.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
        delete.backgroundColor = UIColor.clearColor()
        delete.layer.borderWidth = 1.0
        delete.layer.borderColor = UIColor(white: 1.0, alpha: borderAlpha).CGColor
        delete.layer.cornerRadius = cornerRadius
    }
}

およびUIVisualEffectView:

override func viewDidLoad() {
    super.viewDidLoad()

    let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light))
    visualEffectView.frame = delete.frame
    visualEffectView.bounds = delete.bounds
    view.addSubview(visualEffectView)
}

これにより、ボタンと同じサイズのぼやけたビューが生成されますが、ボタンの上にはありません。質問 2) どうにかして自動レイアウト制約も渡す必要がありますか?

助けてくれてありがとう。

4

1 に答える 1

2

これは、鮮やかな効果を持つ UIVisualEffectView を挿入するために使用できる簡単な例です。

let button = UIButton(frame: CGRect(x: 0, y: 0, width: 500, height: 100))
button.setTitle("Visual Effect", forState: .Normal)

let gradientLayer = CAGradientLayer()
gradientLayer.colors = [UIColor.blueColor().CGColor, UIColor.redColor().CGColor, UIColor.brownColor().CGColor, UIColor.greenColor().CGColor, UIColor.magentaColor().CGColor, UIColor.purpleColor().CGColor, UIColor.cyanColor().CGColor]
gradientLayer.locations = [0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1]
button.layer.insertSublayer(gradientLayer, atIndex: 0)
gradientLayer.frame = button.bounds

let containerEffect = UIBlurEffect(style: .Dark)
let containerView = UIVisualEffectView(effect: containerEffect)
containerView.frame = button.bounds

containerView.userInteractionEnabled = false // Edit: so that subview simply passes the event through to the button

button.insertSubview(containerView, belowSubview: button.titleLabel!)
button.titleLabel?.font = UIFont.boldSystemFontOfSize(30)

let vibrancy = UIVibrancyEffect(forBlurEffect: containerEffect)
let vibrancyView = UIVisualEffectView(effect: vibrancy)
vibrancyView.frame = containerView.bounds
containerView.contentView.addSubview(vibrancyView)

vibrancyView.contentView.addSubview(button.titleLabel!)

そして、これが私がやったことです、

  • ボタンを作成します。
  • グラデーション レイヤーを作成し、ボタンの 0 インデックスに追加します。
  • UIBlurEffectDark の効果を持つコンテナー ビューを作成します。
  • コンテナー ビューをボタンに追加します。
  • 同じ効果を持つ鮮やかなビューを作成し、上記の containerView の contentView に追加します。
  • ラベルをボタンから活気のあるビューのタイトルに移動します

そして、これが最終結果です。titleLabel テキストは、鮮やかさと適用された効果とうまく調和しています。

ここに画像の説明を入力

于 2016-02-23T13:39:17.000 に答える