我有从UIScrollView继承的自定义类,其中包含“关闭按钮”视图。按“关闭按钮”,我想进行动画缩放转换,然后从SuperView中删除整个视图。
class AddReview : UIScrollView {
override init(frame: CGRect){
super.init(frame: frame)
let closeButton = CloseButtonView()
closeButton.frame = CGRect(x:frame.maxX - 50, y:0, width: 50, height: 50)
self.addSubview(closeButton)
let tapCloseGestureRecognizer = UITapGestureRecognizer(target:self, action:#selector(closeButtonPressed))
closeButton.isUserInteractionEnabled = true
closeButton.addGestureRecognizer(tapCloseGestureRecognizer)
}
func closeButtonPressed(){
UIView.animate(withDuration: 0.3){
self.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
}
UIView.animate(withDuration: 0.2, delay: 0.4, animations: { () -> Void in
self.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
}, completion: { (finished: Bool) -> Void in
if (finished)
{
//self.removeFromSuperview()
}
})
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我的问题是没有动画发生。视图会立即被删除,或者当removeFromSuperview
被注释掉时,它会被调整为10%而没有动画。
我尝试过使用layoutIfNeeded
,主要队列的调度以及其他很多东西,但是没有工作。
更重要的是,我注意到它有时会起作用,但大部分时间都不起作用!
知道可能有什么问题吗? 非常感谢任何建议:)
答案 0 :(得分:0)
正如Magnas所说,第二个动画在第一个动画有机会完成之前被调用。尝试:
UIView.animate(withDuration: 0.3, animations: { () -> Void in
self.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
}, completion: { (finished: Bool) -> Void in
// wait for first animation to complete
UIView.animate(withDuration: 0.2, animations: { () -> Void in
self.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
}, completion: { (finished: Bool) -> Void in
if (finished)
{
//self.removeFromSuperview()
}
})
})
这可以略微缩短:
UIView.animate(withDuration: 0.3, animations: {
self.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
}, completion: { (finished: Bool) -> Void in
// wait for first animation to complete
UIView.animate(withDuration: 0.2, animations: {
self.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
}, completion: { finished in
if (finished)
{
//self.removeFromSuperview()
}
})
})