我有一个要在其他课程文件中使用的功能
我声明函子在外部,该方法我想在其他两个类文件中使用它,但是它不起作用,是我错了吗?
class BlurEffect{
var blurEffectView: UIVisualEffectView!
func blurEffectAnimation(isBlur: Bool, view: UIView) {
if isBlur {
let blurEffect = UIBlurEffect(style: .light)
blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(blurEffectView)
UIView.animate(withDuration: 1.0){
self.blurEffectView.alpha = 0.4
self.blurEffectView.transform = CGAffineTransform.identity
}
} else {
blurEffectView.removeFromSuperview()
blurEffectView.effect = nil
}
}
}
我在此类别中声明了BlurEffect方法
class ItemCardTableViewController: UITableViewController {
var blurEffectView: BlurEffect?
@objc func addNewItem() {
blurEffectView?.blurEffectAnimation(isBlur: true)
let addAlert = UIAlertController(title: "", message: "", preferredStyle: .alert)
addAlert.addTextField { (textfied: UITextField) in
textfied.addTarget(addAlert, action: #selector(addAlert.textDidChanged), for: .editingChanged)
}
let confirm = UIAlertAction(title: "ok", style: .default) { (action: UIAlertAction) in
guard let title = addAlert.textFields?.first?.text else { return }
let newItem = ItemCard(title: title, isFinish: false)
self.dataManager.items.append(newItem)
let indexPath = IndexPath(row: self.itemCardTableView.numberOfRows(inSection: 0), section: 0)
self.itemCardTableView.insertRows(at: [indexPath], with: .left)
self.blurEffectView?.blurEffectAnimation(isBlur: false)
}
let cancel = UIAlertAction(title: "cancle", style: .cancel) { _ in self.blurEffectView?.blurEffectAnimation(isBlur: false)}
addAlert.addAction(cancel)
confirm.isEnabled = false
addAlert.addAction(confirm)
self.present(addAlert, animated: true, completion: nil)
}
}
blurEffectView?.blurEffectAnimation(isBlur: true)
不起作用