Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. // TextView subclass that replicates TVOS movies app
  2. // Also made a quick presentation controller
  3. // Just connect the delegate to the ViewController in IB
  4. // and set the TextView class to FocusTextView
  5.  
  6. import UIKit
  7.  
  8. class TextPresentationViewController:UIViewController {
  9. let label = UILabel()
  10. let blurStyle = UIBlurEffectStyle.Dark
  11.  
  12. override func viewDidLoad() {
  13.  
  14. let blurEffect = UIBlurEffect(style: blurStyle)
  15. let blurEffectView = UIVisualEffectView(effect: blurEffect)
  16. blurEffectView.frame = view.bounds
  17. view.addSubview(blurEffectView)
  18.  
  19. let vibrancyEffect = UIVibrancyEffect(forBlurEffect: blurEffect)
  20. let vibrancyEffectView = UIVisualEffectView(effect: vibrancyEffect)
  21. view.addSubview(vibrancyEffectView)
  22.  
  23. vibrancyEffectView.addSubview(label)
  24.  
  25. label.numberOfLines = 0
  26. label.translatesAutoresizingMaskIntoConstraints = false
  27.  
  28. label.numberOfLines = 0
  29. view.addConstraints([
  30. NSLayoutConstraint(item: label, attribute: .Width, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: 0.5, constant: 0),
  31. NSLayoutConstraint(item: label, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1, constant: 0),
  32. NSLayoutConstraint(item: label, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1, constant: 0)])
  33. }
  34. }
  35.  
  36. class FocusTextView: UITextView {
  37.  
  38. private let blurEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .ExtraLight))
  39.  
  40. private let motionEffectGroup = UIMotionEffectGroup()
  41.  
  42. required init?(coder aDecoder: NSCoder) {
  43. super.init(coder: aDecoder)
  44.  
  45. selectable = true
  46. scrollEnabled = false
  47. clipsToBounds = false
  48. textContainer.lineBreakMode = .ByTruncatingTail
  49. textContainerInset = UIEdgeInsetsZero;
  50.  
  51. blurEffectView.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.6)
  52. blurEffectView.frame = CGRectInset(bounds, -10, -10)
  53. blurEffectView.alpha = 0
  54. blurEffectView.layer.cornerRadius = 10
  55. blurEffectView.clipsToBounds = true
  56. insertSubview(blurEffectView, atIndex: 0)
  57.  
  58. let tap = UITapGestureRecognizer(target: self, action: "tapped:")
  59. tap.allowedPressTypes = [NSNumber(integer: UIPressType.Select.rawValue)]
  60. addGestureRecognizer(tap)
  61.  
  62. let motionRange = 5
  63.  
  64. let verticalMotionEffect = UIInterpolatingMotionEffect(keyPath: "center.y", type: .TiltAlongVerticalAxis)
  65. verticalMotionEffect.minimumRelativeValue = -motionRange
  66. verticalMotionEffect.maximumRelativeValue = motionRange
  67.  
  68. let tiltAngle = CGFloat(1 * M_PI / 180)
  69.  
  70. var minX = CATransform3DIdentity
  71. minX.m34 = 1.0 / 500
  72. minX = CATransform3DRotate(minX, -tiltAngle, 1, 0, 0)
  73.  
  74. var maxX = CATransform3DIdentity
  75. maxX.m34 = minX.m34
  76. maxX = CATransform3DRotate(maxX, tiltAngle, 1, 0, 0)
  77.  
  78. let verticalTiltEffect = UIInterpolatingMotionEffect(keyPath: "layer.transform", type: .TiltAlongVerticalAxis)
  79. verticalTiltEffect.minimumRelativeValue = NSValue(CATransform3D: minX)
  80. verticalTiltEffect.maximumRelativeValue = NSValue(CATransform3D: maxX)
  81.  
  82. self.motionEffectGroup.motionEffects = [verticalMotionEffect, verticalTiltEffect]
  83. }
  84.  
  85.  
  86. func tapped(gesture: UITapGestureRecognizer) {
  87.  
  88. if let vc = delegate as? UIViewController {
  89. let modal = TextPresentationViewController()
  90. modal.label.attributedText = attributedText
  91. modal.label.textColor = .whiteColor()
  92. modal.modalPresentationStyle = .OverFullScreen
  93. vc.presentViewController(modal, animated: true, completion: nil)
  94. }
  95. }
  96.  
  97. override func canBecomeFocused() -> Bool {
  98. return true
  99. }
  100.  
  101. override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
  102.  
  103. if context.nextFocusedView == self {
  104. coordinator.addCoordinatedAnimations({ () -> Void in
  105. self.blurEffectView.alpha = 1
  106. self.addMotionEffect(self.motionEffectGroup)
  107. }, completion: nil)
  108. } else if context.previouslyFocusedView == self {
  109. coordinator.addCoordinatedAnimations({ () -> Void in
  110. self.blurEffectView.alpha = 0
  111. self.removeMotionEffect(self.motionEffectGroup)
  112. }, completion: nil)
  113. }
  114. }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement