从屏幕右侧到屏幕左侧为UITextView设置动画

时间:2017-07-08 10:30:52

标签: ios swift animation

我在UICollectionViewCell中添加了一个UITextView。 我想通过一些弹簧阻尼从屏幕右侧为UITextView设置动画。问题是当文本视图动画时它首先增加其宽度并将其移动到左侧。这很有趣,因为宽度已经固定。有人遇到过同样的问题吗?

let topTextView: UITextView = {
        let tv = UITextView();
        tv.textColor = UIColor.white;
        tv.backgroundColor = .red;
        tv.text = "Fast, secure banking \n on the go";
        tv.textAlignment = .center;
        tv.font = UIFont.systemFont(ofSize: 22, weight: UIFont.Weight.light)
        tv.translatesAutoresizingMaskIntoConstraints = false;
                tv.isSelectable = false;
                tv.isEditable = false;
        return tv;

    }();

//Set left anchor to right of the screen
    var topTextViewLeftAnchor = topTextView.leftAnchor.constraint(equalTo: self.rightAnchor);
            topTextViewLeftAnchor.isActive = true;
            topTextView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true;
            topTextView.heightAnchor.constraint(equalToConstant:  60).isActive = true;
            topTextView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true;

//Change the left anchor from right to left
    topTextViewLeftAnchor.isActive = false;
            topTextViewLeftAnchor = topTextView.leftAnchor.constraint(equalTo: self.leftAnchor);
            topTextViewLeftAnchor.isActive = true;

    //Animate the UITextView
            UIView.animate(withDuration: 5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
                self.topTextView.layoutIfNeeded()
            }, completion: nil)

0 个答案:

没有答案