2017-03-16 41 views
0

我有自定义类从UIScrollView的,其中包含“关闭按钮”视图继承。通过按下“关闭按钮”,我想进行动画规模转换,然后从SuperView中删除整个视图。迅速UIView.animate跳过动画

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

第二'UIView.animate'不等待,直到第一个已完成。第二个在第一个完成之前开始,所以我猜测,第二个覆盖第一个并删除了视图。如果你想让它们依次运行,你需要链接它们。 – Magnas

回答

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() 
     } 
    }) 
}) 
+0

谢谢Aleks,这似乎工作。但是在调试时不行,一旦调试器被分离,我必须手动关闭应用程序,重新启动应用程序,然后才能正常工作。 –