动画立即发生

时间:2018-08-16 16:51:09

标签: ios swift uiviewanimation

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

存在问题的部分位于函数“ 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")
        }
    }
}

0 个答案:

没有答案