2017-01-07 5 views
0

テキストビューが属するビューのサイズを変更しています。ビューが大きくなったり小さくなったりすると、テキストが揺れます。ビューのサイズ変更時にテキストビューが揺れる

lazy var textview: UITextView = { 
     let textView = UITextView() 
     textView.text = "" 
     textView.font = .systemFont(ofSize: 12, weight: UIFontWeightMedium) 
     textView.isScrollEnabled = false 
     textView.isEditable = false 
     textView.isSelectable = true 
     textView.isUserInteractionEnabled = true 
     textView.translatesAutoresizingMaskIntoConstraints = false 
     textView.textAlignment = .center 
     textView.textColor = .lightGray 
     textView.dataDetectorTypes = .link 
     return textView 
    }() 

私はそれがそうする際に、この

if let window = UIApplication.shared.keyWindow { 
    let statusBarHeight = UIApplication.shared.statusBarFrame.size.height 

    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveLinear, animations: { 

     self.frame = CGRect(x: 0, y: statusBarHeight, width: window.frame.width, height: window.frame.height - statusBarHeight) 

     self.layer.cornerRadius = 0 

     self.layoutIfNeeded() 

    }, completion: nil) 
} 

のようなフルスクリーンに合わせて中だという見解をリサイズよ、ビューは完全に展開したが:テキストビューとの

宣言textviewsテキストは、アニメーションを非常にプロフェッショナルに見せかけるようなバウンスエフェクトです...アドバイスはありますか?

編集:センターテキストの位置合わせオプションを削除したときのようにうまくいきます。テキストセンターを整列させて動作させるにはどうすればよいですか?

+1

これが問題の原因ではありませんが、あなたのコードは意味がないことに注意してください。なぜあなたは 'layoutIfNeeded'を呼び出すフレームを変更していますか? – matt

+0

@matt layoutIfNeededを削除するとアニメーションが滑らかになります。理由はわかりません。ありがとう – Walker

+0

@matt私はtextviewsの制約を削除する必要があります、フレームをアニメーション..そしてreadd?すべてがうまくアニメーション化されます。ちょっと変わったバウンス効果を出すテキストビューのテキストです。 – Walker

答えて

0

編集:これをもう一度見て、UIScrollView animation of height and contentOffset "jumps" content from bottomに基づいたテクニックを使用しようとしました。

ここでは、私のために働いているテキストの配置を中心としたテキストビューの最小限の作業例を示します。

アニメーションをすべての制約ベースまたはすべてのフレームベースにすることをお勧めします。私はコンテナビューフレームを更新することによってアニメーションが駆動されるバージョンを試みましたが、この制約に基づくアプローチでは余りに時間がかかり始めました。

希望これは正しい方向にあなたを指す:)

import UIKit 

class ViewController: UIViewController { 

    lazy var textView: UITextView = { 
     let textView = UITextView() 
     textView.text = "testing text view" 
     textView.textAlignment = .center 
     textView.translatesAutoresizingMaskIntoConstraints = false 
     return textView 
    }() 

    lazy var containerView: UIView = { 
     let view = UIView() 
     view.translatesAutoresizingMaskIntoConstraints = false 
     return view 
    }() 

    var widthConstraint: NSLayoutConstraint! 
    var topAnchor: NSLayoutConstraint! 

    override func viewDidLoad() { 

     view.backgroundColor = .groupTableViewBackground 

     // add container view and constraints 
     view.addSubview(containerView) 
     containerView.frame = view.bounds.insetBy(dx: 100, dy: 200) 
     containerView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
     containerView.heightAnchor.constraint(equalToConstant: 100).isActive = true 

     // keep reference to topAnchor and width as properties to animate 
     topAnchor = containerView.topAnchor.constraint(lessThanOrEqualTo: view.topAnchor, constant: 100) 
     widthConstraint = containerView.widthAnchor.constraint(equalToConstant: 300) 
     topAnchor.isActive = true 
     widthConstraint.isActive = true 

     // add text view to container view and set constraints 
     containerView.addSubview(textView) 
     textView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true 
     textView.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true 
     textView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true 
     textView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true 
    } 

    @IBAction func toggleResize(_ sender: UIButton) { 

     sender.isSelected = !sender.isSelected 

     view.layoutIfNeeded() 
     widthConstraint.constant = sender.isSelected ? view.bounds.width : 300 
     topAnchor.constant = sender.isSelected ? 20 : 100 

     // caculate the textView content offset for starting position based on 
     // expected end position at end of the animation 
     let xOffset = (textView.bounds.width - widthConstraint.constant)/2 
     textView.contentOffset = CGPoint(x: -xOffset, y: textView.contentOffset.y) 

     UIView.animate(withDuration: 1) { 
      self.view.layoutIfNeeded() 
     } 
    } 
} 
+0

'self.layoutIfNeeded()'のままでアニメーションを変更しても何も変わりません。 'self.layoutIfNeeded()'を削除しても、テキストビューがバウンスすることはありませんが、全体的なアニメーションは荒々しくなります。他のアイデア?ありがとう@MathewS – Walker

+0

@ウォーカー私はもう少し詳しく答えを更新しました – MathewS

+0

私が更新しているフレームは、テキストビューと他の多くのビューを持っているビューに属しています...私はどのようにサイズを変更して表示されませんビューとテキストビューのみがこれを解決します – Walker

関連する問題