将 UIVisualEffectView 添加到按钮
Adding a UIVisualEffectView to button
我有一个透明删除 UIButton
添加到故事板场景,它有一个图片作为背景,我已经添加了一些自动布局约束。
我希望按钮背景是 UIVisualEffectView
所以我想以编程方式将视图添加到按钮所在的位置,然后删除按钮并将其添加回去,使其位于 [=13 之上=].
问题 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) 我是否也需要以某种方式传递自动布局约束?
在此先感谢您的帮助。
这是一个简单的示例,您可以使用它来插入具有活力效果的 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 效果的容器视图。
- 将容器视图添加到按钮。
- 创建一个同样效果的vibrancy view添加到上面containerView的contentView中
- 将标签从按钮移动到活力视图的标题
然后,这是最终结果。 titleLabel 文本与活力和应用的效果很好地融合在一起。
我有一个透明删除 UIButton
添加到故事板场景,它有一个图片作为背景,我已经添加了一些自动布局约束。
我希望按钮背景是 UIVisualEffectView
所以我想以编程方式将视图添加到按钮所在的位置,然后删除按钮并将其添加回去,使其位于 [=13 之上=].
问题 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) 我是否也需要以某种方式传递自动布局约束?
在此先感谢您的帮助。
这是一个简单的示例,您可以使用它来插入具有活力效果的 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 效果的容器视图。
- 将容器视图添加到按钮。
- 创建一个同样效果的vibrancy view添加到上面containerView的contentView中
- 将标签从按钮移动到活力视图的标题
然后,这是最终结果。 titleLabel 文本与活力和应用的效果很好地融合在一起。