首页 > 解决方案 > 动画立即发生

问题描述

我创建了一个自定义表格视图单元格,其中包含用户可以点击和突出显示的图片。我还希望突出显示从中心径向动画,直到它覆盖整个按钮。但是,当按下“按钮”(只是图像视图)时,动画会立即发生并且没有动画。所有代码都被正确执行,但只是立即发生,而不是正确的持续时间。有人有想法么?谢谢。

存在问题的部分是在“presetTapped”函数下,以及“if sender == self.flatTap”条件下。“animatedHighlightView”从大小为 0,0 的超级视图的中心开始。然后它会增长到超级视图的大小。但它不是动画,而是立即发生。

import UIKit

class PresetsTableViewCell: UITableViewCell {
    @IBOutlet weak var flatButtonView: UIView!
    @IBOutlet weak var antiSnoreButtonView: UIView!
    @IBOutlet weak var zeroGravityButtonView: UIView!

    @IBOutlet weak var flatButtonBackgroundView: UIView!
    @IBOutlet weak var antiSnoreButtonBackgroundView: UIView!
    @IBOutlet weak var zeroGravityButtonBackgroundView: UIView!

    var flatTap:UITapGestureRecognizer!
    var antiSnoreTap:UITapGestureRecognizer!
    var zeroGravityTap:UITapGestureRecognizer!
    var selectTap:UITapGestureRecognizer!
    var index:Int = -1

    var animatedHighlightView:UIView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.flatButtonView.layer.cornerRadius = 5
        self.antiSnoreButtonView.layer.cornerRadius = 5
        self.zeroGravityButtonView.layer.cornerRadius = 5

        self.flatButtonBackgroundView.layer.cornerRadius = 5
        self.antiSnoreButtonBackgroundView.layer.cornerRadius = 5
        self.zeroGravityButtonBackgroundView.layer.cornerRadius = 5

        self.flatTap = UITapGestureRecognizer(target: self, action: #selector (self.presetTapped(_:)))
        self.flatButtonView.addGestureRecognizer(self.flatTap)
        self.antiSnoreTap = UITapGestureRecognizer(target: self, action: #selector (self.presetTapped(_:)))
        self.antiSnoreButtonView.addGestureRecognizer(self.antiSnoreTap)
        self.zeroGravityTap = UITapGestureRecognizer(target: self, action: #selector (self.presetTapped(_:)))
        self.zeroGravityButtonView.addGestureRecognizer(self.zeroGravityTap)

        self.animatedHighlightView = UIView(frame: CGRect(x: self.flatButtonBackgroundView.frame.origin.x + self.flatButtonBackgroundView.frame.width/2, y: self.flatButtonBackgroundView.frame.origin.y + self.flatButtonBackgroundView.frame.height/2, width: 0, height: 0))
        self.animatedHighlightView.layer.cornerRadius = self.animatedHighlightView.frame.height/2
        self.animatedHighlightView.backgroundColor = kHighlightPrimary
        self.contentView.addSubview(self.animatedHighlightView)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func presetTapped(_ sender:UITapGestureRecognizer){
        let tb = self.superview as! DashboardTableView
        tb.selectedType = DashboardItems.Presets
        tb.reloadData()
        if sender == self.flatTap {
            UIView.animate(withDuration: 2, delay: 0, options: .curveLinear, animations: {
                self.animatedHighlightView.frame = CGRect(x: self.flatButtonBackgroundView.frame.origin.x, y: self.flatButtonBackgroundView.frame.origin.y, width: self.flatButtonBackgroundView.frame.width, height: self.flatButtonBackgroundView.frame.height)
                self.animatedHighlightView.layer.cornerRadius = self.animatedHighlightView.frame.height/2
            }) { (complete) in
                self.antiSnoreButtonBackgroundView.backgroundColor = kBackgroundColor
                self.zeroGravityButtonBackgroundView.backgroundColor = kBackgroundColor
                tb.reloadData()
            }
            print("dashboard Flat tapped")
        } else if sender == self.antiSnoreTap {
            self.animatedHighlightView.frame = CGRect(x: self.flatButtonBackgroundView.frame.origin.x + self.flatButtonBackgroundView.frame.width/2, y: self.flatButtonBackgroundView.frame.origin.y + self.flatButtonBackgroundView.frame.height/2, width: 0, height: 0)
            self.flatButtonBackgroundView.backgroundColor = kBackgroundColor
            self.antiSnoreButtonBackgroundView.backgroundColor = kHighlightPrimary
            self.zeroGravityButtonBackgroundView.backgroundColor = kBackgroundColor
            tb.reloadData()
            print("dashboard Anti-Snore tapped")
        } else if sender == self.zeroGravityTap {
            self.flatButtonBackgroundView.backgroundColor = kBackgroundColor
            self.antiSnoreButtonBackgroundView.backgroundColor = kBackgroundColor
            self.zeroGravityButtonBackgroundView.backgroundColor = kHighlightPrimary
            tb.reloadData()
            print("dashboard Zero Gravity tapped")
        }
    }
}

标签: iosswiftuiviewanimation

解决方案


推荐阅读