gpt4 book ai didi

ios - 以编程方式更改现有的 UIBlurEffect UIBlurEffectStyle?

转载 作者:行者123 更新时间:2023-11-29 02:18:14 26 4
gpt4 key购买 nike

我在 Storyboard上有一个视觉效果 View 作为导出连接到我的 ViewController。效果是在它后面打上一个 ImageView 并且效果很好。我正在尝试在单击 IBAction 的按钮内将 UIBlurEffectStyle 从 Light 更改为 Dark。如有任何帮助,我们将不胜感激!

@IBOutlet weak var blurView: UIVisualEffectView!

override func viewDidLoad() {
super.viewDidLoad()
}

@IBAction func changeBlurView() {

// This is wrong, but is my best attempt so far.
self.blurView(UIBlurEffectStyle.Dark)

}

最佳答案

在创建自己的应用程序时,我遇到了类似的问题。我根本不使用IB,所以一切都是以编程方式完成的。我查看了 UIVisualEffectView.h,它没有提供任何动态效果更改(希望这将来会改变)。

这是我的解决方案(我使用的是最新的 Swift 版本,因此有一个 as! 运算符):

class CustomVisualEffectView : UIVisualEffectView
{
deinit
{
println("UIVisualEffectView will be destroyed.")
}
}

class ViewController: UIViewController
{
var _blurEffect = UIBlurEffect() // global so you can use it for vibrancy effect view as well
var _blurredEffectView = CustomVisualEffectView()

let _someSubView = UIView()
let _someOtherSubView = UIView()

let _effectChanger = UIButton.buttonWithType(.System) as! UIButton

override func viewDidLoad()
{
super.viewDidLoad()

self.view.backgroundColor = UIColor.orangeColor()

/* create a button to change the effect */
_effectChanger.setTitle("Change effect!", forState: UIControlState.Normal)
_effectChanger.frame.size = CGSize(width: 100, height: 20)
_effectChanger.center = self.view.center
_effectChanger.addTarget(self, action: Selector("buttonClicked"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(_effectChanger)

/* here is our effect view */
_blurEffect = UIBlurEffect(style: self.randomBlurEfffectStyle())
_blurredEffectView = CustomVisualEffectView(effect: _blurEffect)
self.layoutEffectView()
self.view.addSubview(_blurredEffectView)

/* create two subviews and put them on the effect view */
_someSubView.frame = CGRectMake(10, 10, 10, 10)
_someSubView.backgroundColor = UIColor.redColor()
_blurredEffectView.contentView.addSubview(_someSubView)

_someOtherSubView.frame = CGRectMake(30, 30, 10, 10)
_someOtherSubView.backgroundColor = UIColor.greenColor()
_blurredEffectView.contentView.addSubview(_someOtherSubView)
}

func layoutEffectView()
{
_blurredEffectView.frame.size = CGSize(width: 100, height: 80)
_blurredEffectView.center = CGPointMake(_effectChanger.center.x, _effectChanger.center.y - 50)
}

func buttonClicked()
{
var tempArray = [AnyObject]()

/* get all subviews from the effect view */
for view in _blurredEffectView.contentView.subviews
{
tempArray.append(view)
view.removeFromSuperview()
}
/* modify your effect view */
_blurEffect = UIBlurEffect(style: self.randomBlurEfffectStyle())

/* IMPORTANT: so the old effect view can be destroyed by arc */
_blurredEffectView.removeFromSuperview()

_blurredEffectView = CustomVisualEffectView(effect: _blurEffect)
/* I think this will be really tricky if you will use auto layout */
self.layoutEffectView()
self.view.addSubview(_blurredEffectView)

/* put all subviews back to the effect view*/
for view in tempArray
{
_blurredEffectView.contentView.addSubview(view as! UIView)
}
}

func randomBlurEfffectStyle() -> UIBlurEffectStyle
{
let randomBlurEffectStyle : UIBlurEffectStyle

switch Int(arc4random_uniform(3)) // [0,1,2]
{
case 0:
randomBlurEffectStyle = .ExtraLight
case 1:
randomBlurEffectStyle = .Light
default:
randomBlurEffectStyle = .Dark
}

return randomBlurEffectStyle
}
}

关于ios - 以编程方式更改现有的 UIBlurEffect UIBlurEffectStyle?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28488661/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com