簡體   English   中英

UIView動畫在iOS 8中無法正常工作?

[英]UIView animation is not work properly in iOS 8?

我想創建如下動畫,我在iOS 11.2中完成了此操作。

工作動畫

我在iOS 8.4和9.2中測試了- broke

動畫損壞

故事板

我用了autoLaout 在此處輸入圖片說明

圓形陰影按鈕

我將UIButton子類化。 向其中添加了RoundedCorner和DropShadow協議。 它包含一個關心動畫的animateButton函數。

class RoundedShadowButton: UIButton, RoundedCorner, DropShadow {
    var originalSize: CGRect?

    func animateButton(shouldLoad: Bool, withMessage message: String?) {
        let spinner = UIActivityIndicatorView()
        spinner.activityIndicatorViewStyle = .whiteLarge
        spinner.color = .darkGray
        spinner.alpha = 0.0
        spinner.hidesWhenStopped = true
        spinner.tag = 21

        if shouldLoad {
            self.addSubview(spinner)
            self.setTitle("", for: .normal)
            UIView.animate(withDuration: 0.2, animations: {
                self.setRoundedCorners(radius: self.frame.height / 2)
                self.frame = CGRect(x: self.frame.midX - (self.frame.height / 2), y: self.frame.origin.y, width: self.frame.height, height: self.frame.height)
            }, completion: { (finished) in
                if finished {
                    spinner.startAnimating()
                    spinner.center = CGPoint(x: self.frame.width / 2 + 1, y: self.frame.width / 2 + 1)
                    spinner.fadeTo(alphaValue: 1.0, withDuration: 0.2)
                }
            })
            self.isUserInteractionEnabled = false
        } else {
            self.isUserInteractionEnabled = true

            // remove spinner
            for subView in self.subviews {
                if subView.tag == 21 {
                    subView.removeFromSuperview()
                }
            }

            // return back to original button
            UIView.animate(withDuration: 0.2, animations: {
                if let size = self.originalSize {
                    self.setRoundedCorners(radius: 8)
                    self.frame = size
                    self.setTitle(message, for: .normal)
                }
            })
        }
    }
}

圓角

protocol RoundedCorner {}

extension RoundedCorner where Self: UIView {
    func setRoundedCorners(radius: CGFloat) {
        layer.cornerRadius = radius
    }
}

陰影

protocol DropShadow {}

extension DropShadow where Self: UIView {
    func setShadow(width: CGFloat = 0, height: CGFloat = 0, opacity: Float, radius: CGFloat, color: UIColor) {
        layer.shadowColor = color.cgColor
        layer.shadowOpacity = opacity
        layer.shadowOffset = CGSize(width: width, height: height)
        layer.shadowRadius = radius
    }
}

家用VC

在HomeVC的viewDidAppear中,我設置拐角半徑,陰影和框架大小。 在按鈕IBAction中,我調用了動畫功能。

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        actionBtn.originalSize = actionBtn.frame
        actionBtn.setRoundedCorners(radius: 8)
        actionBtn.setShadow(opacity: 0.3, radius: 10.0, color: .darkGray)
    }

@IBAction func actionBtnTapped(_ sender: Any) {
        self.actionBtn.animateButton(shouldLoad: true, withMessage: nil)
    }

如何解決呢?

使用約束進行動畫處理時,還需要使用約束。 盡管有時可能會起作用,但您不能依靠它。 您可以這樣解決。

  • 刪除leadingtrailing約束。
  • 添加一個寬度約束,其same widthsuperview減去40 same width 連接這類型的類變量NSLayoutConstraint命名widthConstraint 設置優先級750
  • 添加另一個寬度約束,該約束的width設置為與高度相同,從而變為圓形。 將優先級設置為500
  • 將約束添加到superview center

現在,在您的代碼中替換框架修改部分。

            self.frame = CGRect(x: self.frame.midX - (self.frame.height / 2), y: self.frame.origin.y, width: self.frame.height, height: self.frame.height)

有了這個。 通過更改優先級,您可以選擇應該生效的約束。 由於寬約束為750,它將克服窄約束。 將優先級更改為250時,窄約束將獲勝。

            self.widthConstraint.priority = 250
            self.layoutIfNeeded()

試試這個它在我的工作結束,只需禁用此:

   self.view.translatesAutoresizingMaskIntoConstraints  = false

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM