ボタンをクリックすると、下からサブビューを取得しようとしています。しかし、ボタンがクリック可能なのは初めてです。アニメーションの後の2回目のクリックではクリックできません。UIButtonは最初のクリックの後にクリック可能ではありません
ここにコードがあります。画面が最初に表示されたら
class AnimateView: UIView {
var button: UIButton!
var menuView: UIView!
var mainView: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
mainView = UIView(frame: CGRect(x: 0, y: 0, width:
self.frame.size.width, height: self.frame.size.height))
mainView.clipsToBounds = true
mainView.backgroundColor = .clear
mainView.isUserInteractionEnabled = true
mainView.isExclusiveTouch = true
self.addSubview(mainView)
let theRect = CGRect(x: self.frame.size.width/2 - 44/2, y: 0,
width: 44, height: 44)
button = UIButton(frame: theRect)
check.layer.cornerRadius = btnView.frame.size.height/2
mainView.addSubview(self.check)
mainView.bringSubview(toFront: check)
check.addTarget(self, action: #selector(buttonClick(_:)),
for:.touchUpInside)
let newRect = CGRect(x: 0, y: check.frame.height, width:
self.frame.size.width, height: self.mainView.frame.size.height)
menuView = UIView(frame: newRect)
menuView.backgroundColor = .blue
mainView.addSubview(menuView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func buttonClick(_ sender: UIButton) {
toggleMenu()
}
func toggleMenu() {
if check.transform == CGAffineTransform.identity {
UIView.animate(withDuration: 1, animations: {
self.check.transform = CGAffineTransform(scaleX: 12, y: 12)
self.mainView.transform = CGAffineTransform(translationX: 0, y: -120)
self.check.transform = CGAffineTransform(rotationAngle: self.radians(180)) // 180 is 3.14 radian
self.setNeedsLayout()
}) { (true) in
UIView.animate(withDuration: 0.5, animations: {
})
}
} else {
UIView.animate(withDuration: 1, animations: {
// .identity sets to original position
//self.btnView.transform = .identity
self.mainView.transform = .identity
self.button.transform = .identity
})
}
}
}
このクラスは、この
class MainViewClass: UIView {
var animateView: AnimateView!
override init(frame: CGRect) {
animateView = AnimateView(frame: CGRect(x: 0, y: self.frame.size.height - 44 - 44 - 20, width: self.frame.size.width, height: 122+44))
//animateView.clipsToBounds = true
self.addSubview(animateView)
self.bringSubview(toFront: animateView)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
のような別のUIViewクラスから呼び出されます。
最初のボタンがクリック可能で、それはそれをクリックされたときサブビューに沿って上に移動します。
ボタンはクリックできません。ボタンの枠を見たとき、上に上がってもボタンが画面の底に触れて画面が最初に表示されたときと同じです。 私が意図しているのは、ボタンをクリックしたときに下からアニメーションのある画面を表示し、2回目のクリックでデフォルトの位置に移動することです。
ボタンをmainviewのサブビューではなく、selfの中にサブビューとして配置すると、ボタンはクリック可能ですが、ビューがアニメーション化されて上に移動した後も同じ位置に残ります。
self.addSubview(ボタン)
あなたはそれがアニメーション化完了だ後、あなたの 'AnimateView'を削除する必要がありますので、ボタンが再び公開されますか? –
ボタンを最初にクリックすると、サブビューが画面の下からアニメーション表示され、ボタンも上に移動する必要があります。次に、2回目のクリックで、ボタンがサブビューとともに表示されます。 今、ボタンをクリックすると、サブビューが上に移動し、ボタンのボタンはクリックできません。 – bthapa
ボタンをクリックして上に移動すると、クリックできません。私はフレームを更新してみましたが、クリックできませんでした。 – bthapa