You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.3 KiB

  1. // The MIT License (MIT)
  2. //
  3. // Copyright (c) 2016 Luke Zhao <me@lkzhao.com>
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. import UIKit
  23. class SourcePreprocessor: BasePreprocessor {
  24. override func process(fromViews: [UIView], toViews: [UIView]) {
  25. for fv in fromViews {
  26. guard let id = context[fv]?.source,
  27. let tv = context.destinationView(for: id) else { continue }
  28. prepareFor(view: fv, targetView: tv)
  29. }
  30. for tv in toViews {
  31. guard let id = context[tv]?.source,
  32. let fv = context.sourceView(for: id) else { continue }
  33. prepareFor(view: tv, targetView: fv)
  34. }
  35. }
  36. func prepareFor(view: UIView, targetView: UIView) {
  37. let targetPos = context.container.convert(targetView.layer.position, from: targetView.superview!)
  38. let targetTransform = context.container.layer.flatTransformTo(layer: targetView.layer)
  39. var state = context[view]!
  40. // use global coordinate space since over target position is converted from the global container
  41. state.coordinateSpace = .global
  42. state.position = targetPos
  43. state.transform = targetTransform
  44. // remove incompatible options
  45. state.size = nil
  46. if view.bounds.size != targetView.bounds.size {
  47. state.size = targetView.bounds.size
  48. }
  49. if state.cornerRadius == nil, view.layer.cornerRadius != targetView.layer.cornerRadius {
  50. state.cornerRadius = targetView.layer.cornerRadius
  51. }
  52. if view.layer.shadowColor != targetView.layer.shadowColor {
  53. state.shadowColor = targetView.layer.shadowColor
  54. }
  55. if view.layer.shadowOpacity != targetView.layer.shadowOpacity {
  56. state.shadowOpacity = targetView.layer.shadowOpacity
  57. }
  58. if view.layer.shadowOffset != targetView.layer.shadowOffset {
  59. state.shadowOffset = targetView.layer.shadowOffset
  60. }
  61. if view.layer.shadowRadius != targetView.layer.shadowRadius {
  62. state.shadowRadius = targetView.layer.shadowRadius
  63. }
  64. if view.layer.shadowPath != targetView.layer.shadowPath {
  65. state.shadowPath = targetView.layer.shadowPath
  66. }
  67. if view.layer.contentsRect != targetView.layer.contentsRect {
  68. state.contentsRect = targetView.layer.contentsRect
  69. }
  70. if view.layer.contentsScale != targetView.layer.contentsScale {
  71. state.contentsScale = targetView.layer.contentsScale
  72. }
  73. context[view] = state
  74. }
  75. }