2015-03-03 75 views
1

我有一個工作的ViewController與工作UIBlurEffect。這裏是我的代碼:如何將UIVibrancyEffect添加到現有的UILabel(IBOutlet)?

@IBOutlet weak var nameLabel: UILabel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let blurEffect = UIBlurEffect(style: .Light) 
     let blurView = UIVisualEffectView(effect: blurEffect) 
     blurView.frame = self.view.frame 
     blurView.setTranslatesAutoresizingMaskIntoConstraints(false) 
     self.view.insertSubview(blurView, atIndex: 0) 
} 

現在我想添加一個UIVibranyEffect到nameLabel。 如何以編程方式將UIVibrancyEffect添加到現有的UILabel?

回答

4

你需要實例化一個UIVisualEffectView並添加任何你想在裏面充滿活力。例如:

// Blur Effect 
    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark) 
    let blurEffectView = UIVisualEffectView(effect: blurEffect) 
    blurEffectView.frame = view.bounds 
    view.addSubview(blurEffectView) 

    // Vibrancy Effect 
    let vibrancyEffect = UIVibrancyEffect(forBlurEffect: blurEffect) 
    let vibrancyEffectView = UIVisualEffectView(effect: vibrancyEffect) 
    vibrancyEffectView.frame = view.bounds 

    // Label for vibrant text 
    let vibrantLabel = UILabel() 
    vibrantLabel.text = "Vibrant" 

    // Add label to the vibrancy view 
    vibrancyEffectView.contentView.addSubview(vibrantLabel) 

    // Add the vibrancy view to the blur view 
    blurEffectView.contentView.addSubview(vibrancyEffectView)