http://www.cocoachina.com/ios/20141223/10731.html 这篇文章里有关于更多高斯毛玻璃效果的做法。
利用UIToolbar来实现的一个带有跟系统一样毛玻璃效果的View,可直接在上面增加Label,Button。。。 等控件
为了不跟 Subview混淆使用,所以 addSublayer。(用addSubview可以实现一样的效果,还可以用autoresizingMask 不用重载 layoutSubviews()
代码很少,直接上!
import UIKit
/**
* 毛玻璃效果的View
*/
class FuzzyView : UIView {
private let view: UIToolbar!
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.clearColor()
self.view = UIToolbar(frame: self.bounds)
self.view.backgroundColor = UIColor.clearColor()
self.view.userInteractionEnabled = false
self.layer.addSublayer(self.view.layer)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
self.view.frame = self.bounds
}
//MARK:- Touch Event
// 让其支持拖动
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
if touches.count >= 1 {
let touch = touches.anyObject() as UITouch
let pointPrevious = touch.previousLocationInView(self)
let point = touch.locationInView(self)
var center = self.center
center.x += point.x - pointPrevious.x
center.y += point.y - pointPrevious.y
self.center = center
}
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {}
override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) {
self.touchesEnded(touches, withEvent: event)
}
}