Advertisement
Don_Mag

Text View Rotate

Jul 28th, 2022
1,609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.38 KB | None | 0 0
  1. class RotateTextViewVC: UIViewController {
  2.    
  3.     let tv = UITextView()
  4.    
  5.     override func viewDidLoad() {
  6.         super.viewDidLoad()
  7.         view.backgroundColor = .systemYellow
  8.  
  9.         tv.isScrollEnabled = false
  10.         tv.text = "This is some sample text for the text view."
  11.         tv.font = .systemFont(ofSize: 24.0, weight: .regular)
  12.  
  13.         // add a slider to rotate the text view
  14.         let slider = UISlider()
  15.         slider.addTarget(self, action: #selector(sliderChanged(_:)), for: .valueChanged)
  16.        
  17.         slider.translatesAutoresizingMaskIntoConstraints = false
  18.         view.addSubview(slider)
  19.        
  20.         tv.translatesAutoresizingMaskIntoConstraints = false
  21.         view.addSubview(tv)
  22.        
  23.         let g = view.safeAreaLayoutGuide
  24.         NSLayoutConstraint.activate([
  25.             slider.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0),
  26.             slider.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0),
  27.             slider.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0),
  28.  
  29.             tv.centerXAnchor.constraint(equalTo: view.centerXAnchor),
  30.             tv.centerYAnchor.constraint(equalTo: view.centerYAnchor),
  31.             tv.widthAnchor.constraint(equalToConstant: 200.0),
  32.         ])
  33.        
  34.         slider.value = 0.5
  35.     }
  36.     @objc func sliderChanged(_ sender: UISlider) {
  37.  
  38.         let deg: CGFloat = (CGFloat(sender.value) - 0.5) * 720.0
  39.         let radians = deg * .pi / 180.0
  40.         tv.layer.transform = CATransform3DRotate(CATransform3DIdentity, radians, 0, 0, 1)
  41.  
  42.     }
  43.    
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement