BlurButton.swift 1.37 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
//  BlurButton.swift
//  Amiv
//
//  Created by Domenic Wüthrich on 20.09.18.
//  Copyright © 2018 Amiv an der ETH. All rights reserved.
//

import Foundation
import UIKit

public class BlurButton: UIButton {
    
    // MARK: - Variables
    
    public let style: UIBlurEffect.Style
    
    // MARK: - View Variables
    
    public private(set) var blurBackgroundView: UIVisualEffectView!
    
    // MARK: - Initializers
    
    public init(style: UIBlurEffect.Style) {
        self.style = style
        super.init(frame: .null)
        
        self.blurBackgroundView = self.createBackgroundLayer(style)
        self.insertSubview(self.blurBackgroundView, at: 0)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - View Creation
    
    private func createBackgroundLayer(_ style: UIBlurEffect.Style) -> UIVisualEffectView {
        let blurEffect = UIBlurEffect(style: self.style)
        let blurredEffectView = UIVisualEffectView(effect: blurEffect)
        blurredEffectView.isUserInteractionEnabled = false
        blurredEffectView.isExclusiveTouch = false
        
        return blurredEffectView
    }
    
    // MARK: - Layout
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        
        self.blurBackgroundView.frame = self.bounds
    }
    
}