我正在尝试使用功能appear()
将我的Options类的实例制作成动画到屏幕上,该功能在按下SearchResult
类中的按钮时会激活,但是我遇到了麻烦,该实例已添加为ViewController
类中的视图的子视图。
我已经可以在optionMenu.appear()
中使用函数ViewController
,并且可以正常工作,但是当通过单击SearchResult
中的按钮调用该函数时,它不起作用,除了我执行的动画以外,它似乎无所不能在optionMenu.appear()
中测试了打印,并成功打印。
import Foundation
import UIKit
class SearchResult {
private let link: String
var body = UIView()
init(_ link: String){
self.link = link
let options = UIButton(x: Int(body.bounds.width-60), y: 10, size: 50.0)
options.addTarget(self, action: #selector(optionsClicked), for: .touchUpInside)
body.addSubview(options)
}
@objc func optionsClicked(){
ViewController().optionMenu.appear()
}
}
import Foundation
import UIKit
class Options: UIView{
var buttons = [UIButton]()
private var link = String()
init(_ titles: [String]){
let height = ((titles.count+1)*65)+5
super.init(frame: CGRect(x: 0, y: Int(UIScreen.main.bounds.height)-height, width: Int(UIScreen.main.bounds.width), height: height))
var i = 0
for title in titles{
let button = UIButton(frame: CGRect(x: 5, y: 5+(i*65), width: Int(UIScreen.main.bounds.width)-10, height: 60))
button.setTitle(title, for: .normal)
buttons.append(button)
self.addSubview(button)
i += 1
}
let cancel = UIButton(frame: CGRect(x: 5, y: 5+(i*65), width: Int(UIScreen.main.bounds.width)-10, height: 60))
cancel.setTitle("cancel", for: .normal)
self.addSubview(cancel)
}
func appear(){
UIView.animate(withDuration: 0.2, animations: {
self.transform = CGAffineTransform(translationX: 0, y: -self.bounds.height)
})
}
@objc func disappear(){
UIView.animate(withDuration: 0.2, animations: {
self.transform = CGAffineTransform.identity
})
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
这是因为您正在构造一个新的Viewcontroller,并且每次调用optionsClicked()
时,都会在该Viewcontroller的选项菜单上显示调用。您需要对具有选项菜单的viewController的引用,并执行myViewController.optionMenu.appear()