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.

499 lines
15 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. public final class HeroModifier {
  24. internal let apply:(inout HeroTargetState) -> Void
  25. public init(applyFunction:@escaping (inout HeroTargetState) -> Void) {
  26. apply = applyFunction
  27. }
  28. }
  29. // basic modifiers
  30. extension HeroModifier {
  31. /**
  32. Fade the view during transition
  33. */
  34. public static var fade = HeroModifier { targetState in
  35. targetState.opacity = 0
  36. }
  37. /**
  38. Force don't fade view during transition
  39. */
  40. public static var forceNonFade = HeroModifier { targetState in
  41. targetState.nonFade = true
  42. }
  43. /**
  44. Set the position for the view to animate from/to.
  45. - Parameters:
  46. - position: position for the view to animate from/to
  47. */
  48. public static func position(_ position: CGPoint) -> HeroModifier {
  49. return HeroModifier { targetState in
  50. targetState.position = position
  51. }
  52. }
  53. /**
  54. Set the size for the view to animate from/to.
  55. - Parameters:
  56. - size: size for the view to animate from/to
  57. */
  58. public static func size(_ size: CGSize) -> HeroModifier {
  59. return HeroModifier { targetState in
  60. targetState.size = size
  61. }
  62. }
  63. }
  64. // transform modifiers
  65. extension HeroModifier {
  66. /**
  67. Set the transform for the view to animate from/to. Will override previous perspective, scale, translate, & rotate modifiers
  68. - Parameters:
  69. - t: the CATransform3D object
  70. */
  71. public static func transform(_ t: CATransform3D) -> HeroModifier {
  72. return HeroModifier { targetState in
  73. targetState.transform = t
  74. }
  75. }
  76. /**
  77. Set the perspective on the transform. use in combination with the rotate modifier.
  78. - Parameters:
  79. - perspective: set the camera distance of the transform
  80. */
  81. public static func perspective(_ perspective: CGFloat) -> HeroModifier {
  82. return HeroModifier { targetState in
  83. var transform = targetState.transform ?? CATransform3DIdentity
  84. transform.m34 = 1.0 / -perspective
  85. targetState.transform = transform
  86. }
  87. }
  88. /**
  89. Scale 3d
  90. - Parameters:
  91. - x: scale factor on x axis, default 1
  92. - y: scale factor on y axis, default 1
  93. - z: scale factor on z axis, default 1
  94. */
  95. public static func scale(x: CGFloat = 1, y: CGFloat = 1, z: CGFloat = 1) -> HeroModifier {
  96. return HeroModifier { targetState in
  97. targetState.transform = CATransform3DScale(targetState.transform ?? CATransform3DIdentity, x, y, z)
  98. }
  99. }
  100. /**
  101. Scale in x & y axis
  102. - Parameters:
  103. - xy: scale factor in both x & y axis
  104. */
  105. public static func scale(_ xy: CGFloat) -> HeroModifier {
  106. return .scale(x: xy, y: xy)
  107. }
  108. /**
  109. Translate 3d
  110. - Parameters:
  111. - x: translation distance on x axis in display pixel, default 0
  112. - y: translation distance on y axis in display pixel, default 0
  113. - z: translation distance on z axis in display pixel, default 0
  114. */
  115. public static func translate(x: CGFloat = 0, y: CGFloat = 0, z: CGFloat = 0) -> HeroModifier {
  116. return HeroModifier { targetState in
  117. targetState.transform = CATransform3DTranslate(targetState.transform ?? CATransform3DIdentity, x, y, z)
  118. }
  119. }
  120. public static func translate(_ point: CGPoint, z: CGFloat = 0) -> HeroModifier {
  121. return translate(x: point.x, y: point.y, z: z)
  122. }
  123. /**
  124. Rotate 3d
  125. - Parameters:
  126. - x: rotation on x axis in radian, default 0
  127. - y: rotation on y axis in radian, default 0
  128. - z: rotation on z axis in radian, default 0
  129. */
  130. public static func rotate(x: CGFloat = 0, y: CGFloat = 0, z: CGFloat = 0) -> HeroModifier {
  131. return HeroModifier { targetState in
  132. targetState.transform = CATransform3DRotate(targetState.transform ?? CATransform3DIdentity, x, 1, 0, 0)
  133. targetState.transform = CATransform3DRotate(targetState.transform!, y, 0, 1, 0)
  134. targetState.transform = CATransform3DRotate(targetState.transform!, z, 0, 0, 1)
  135. }
  136. }
  137. public static func rotate(_ point: CGPoint, z: CGFloat = 0) -> HeroModifier {
  138. return rotate(x: point.x, y: point.y, z: z)
  139. }
  140. /**
  141. Rotate 2d
  142. - Parameters:
  143. - z: rotation in radian
  144. */
  145. public static func rotate(_ z: CGFloat) -> HeroModifier {
  146. return .rotate(z: z)
  147. }
  148. }
  149. extension HeroModifier {
  150. /**
  151. Set the opacity for the view to animate from/to.
  152. - Parameters:
  153. - opacity: opacity for the view to animate from/to
  154. */
  155. public static func opacity(_ opacity: CGFloat) -> HeroModifier {
  156. return HeroModifier { targetState in
  157. targetState.opacity = Float(opacity)
  158. }
  159. }
  160. /**
  161. Set the backgroundColor for the view to animate from/to.
  162. - Parameters:
  163. - backgroundColor: backgroundColor for the view to animate from/to
  164. */
  165. public static func backgroundColor(_ backgroundColor: UIColor) -> HeroModifier {
  166. return HeroModifier { targetState in
  167. targetState.backgroundColor = backgroundColor.cgColor
  168. }
  169. }
  170. /**
  171. Set the cornerRadius for the view to animate from/to.
  172. - Parameters:
  173. - cornerRadius: cornerRadius for the view to animate from/to
  174. */
  175. public static func cornerRadius(_ cornerRadius: CGFloat) -> HeroModifier {
  176. return HeroModifier { targetState in
  177. targetState.cornerRadius = cornerRadius
  178. }
  179. }
  180. /**
  181. Set the zPosition for the view to animate from/to.
  182. - Parameters:
  183. - zPosition: zPosition for the view to animate from/to
  184. */
  185. public static func zPosition(_ zPosition: CGFloat) -> HeroModifier {
  186. return HeroModifier { targetState in
  187. targetState.zPosition = zPosition
  188. }
  189. }
  190. /**
  191. Set the contentsRect for the view to animate from/to.
  192. - Parameters:
  193. - contentsRect: contentsRect for the view to animate from/to
  194. */
  195. public static func contentsRect(_ contentsRect: CGRect) -> HeroModifier {
  196. return HeroModifier { targetState in
  197. targetState.contentsRect = contentsRect
  198. }
  199. }
  200. /**
  201. Set the contentsScale for the view to animate from/to.
  202. - Parameters:
  203. - contentsScale: contentsScale for the view to animate from/to
  204. */
  205. public static func contentsScale(_ contentsScale: CGFloat) -> HeroModifier {
  206. return HeroModifier { targetState in
  207. targetState.contentsScale = contentsScale
  208. }
  209. }
  210. /**
  211. Set the borderWidth for the view to animate from/to.
  212. - Parameters:
  213. - borderWidth: borderWidth for the view to animate from/to
  214. */
  215. public static func borderWidth(_ borderWidth: CGFloat) -> HeroModifier {
  216. return HeroModifier { targetState in
  217. targetState.borderWidth = borderWidth
  218. }
  219. }
  220. /**
  221. Set the borderColor for the view to animate from/to.
  222. - Parameters:
  223. - borderColor: borderColor for the view to animate from/to
  224. */
  225. public static func borderColor(_ borderColor: UIColor) -> HeroModifier {
  226. return HeroModifier { targetState in
  227. targetState.borderColor = borderColor.cgColor
  228. }
  229. }
  230. /**
  231. Set the shadowColor for the view to animate from/to.
  232. - Parameters:
  233. - shadowColor: shadowColor for the view to animate from/to
  234. */
  235. public static func shadowColor(_ shadowColor: UIColor) -> HeroModifier {
  236. return HeroModifier { targetState in
  237. targetState.shadowColor = shadowColor.cgColor
  238. }
  239. }
  240. /**
  241. Set the shadowOpacity for the view to animate from/to.
  242. - Parameters:
  243. - shadowOpacity: shadowOpacity for the view to animate from/to
  244. */
  245. public static func shadowOpacity(_ shadowOpacity: CGFloat) -> HeroModifier {
  246. return HeroModifier { targetState in
  247. targetState.shadowOpacity = Float(shadowOpacity)
  248. }
  249. }
  250. /**
  251. Set the shadowOffset for the view to animate from/to.
  252. - Parameters:
  253. - shadowOffset: shadowOffset for the view to animate from/to
  254. */
  255. public static func shadowOffset(_ shadowOffset: CGSize) -> HeroModifier {
  256. return HeroModifier { targetState in
  257. targetState.shadowOffset = shadowOffset
  258. }
  259. }
  260. /**
  261. Set the shadowRadius for the view to animate from/to.
  262. - Parameters:
  263. - shadowRadius: shadowRadius for the view to animate from/to
  264. */
  265. public static func shadowRadius(_ shadowRadius: CGFloat) -> HeroModifier {
  266. return HeroModifier { targetState in
  267. targetState.shadowRadius = shadowRadius
  268. }
  269. }
  270. /**
  271. Set the shadowPath for the view to animate from/to.
  272. - Parameters:
  273. - shadowPath: shadowPath for the view to animate from/to
  274. */
  275. public static func shadowPath(_ shadowPath: CGPath) -> HeroModifier {
  276. return HeroModifier { targetState in
  277. targetState.shadowPath = shadowPath
  278. }
  279. }
  280. /**
  281. Set the masksToBounds for the view to animate from/to.
  282. - Parameters:
  283. - masksToBounds: masksToBounds for the view to animate from/to
  284. */
  285. public static func masksToBounds(_ masksToBounds: Bool) -> HeroModifier {
  286. return HeroModifier { targetState in
  287. targetState.masksToBounds = masksToBounds
  288. }
  289. }
  290. /**
  291. Create an overlay on the animating view.
  292. - Parameters:
  293. - color: color of the overlay
  294. - opacity: opacity of the overlay
  295. */
  296. public static func overlay(color: UIColor, opacity: CGFloat) -> HeroModifier {
  297. return HeroModifier { targetState in
  298. targetState.overlay = (color.cgColor, opacity)
  299. }
  300. }
  301. }
  302. // timing modifiers
  303. extension HeroModifier {
  304. /**
  305. Sets the duration of the animation for a given view. If not used, Hero will use determine the duration based on the distance and size changes.
  306. - Parameters:
  307. - duration: duration of the animation
  308. Note: a duration of .infinity means matching the duration of the longest animation. same as .durationMatchLongest
  309. */
  310. public static func duration(_ duration: TimeInterval) -> HeroModifier {
  311. return HeroModifier { targetState in
  312. targetState.duration = duration
  313. }
  314. }
  315. /**
  316. Sets the duration of the animation for a given view to match the longest animation of the transition.
  317. */
  318. public static var durationMatchLongest: HeroModifier = HeroModifier { targetState in
  319. targetState.duration = .infinity
  320. }
  321. /**
  322. Sets the delay of the animation for a given view.
  323. - Parameters:
  324. - delay: delay of the animation
  325. */
  326. public static func delay(_ delay: TimeInterval) -> HeroModifier {
  327. return HeroModifier { targetState in
  328. targetState.delay = delay
  329. }
  330. }
  331. /**
  332. Sets the timing function of the animation for a given view. If not used, Hero will use determine the timing function based on whether or not the view is entering or exiting the screen.
  333. - Parameters:
  334. - timingFunction: timing function of the animation
  335. */
  336. public static func timingFunction(_ timingFunction: CAMediaTimingFunction) -> HeroModifier {
  337. return HeroModifier { targetState in
  338. targetState.timingFunction = timingFunction
  339. }
  340. }
  341. /**
  342. (iOS 9+) Use spring animation with custom stiffness & damping. The duration will be automatically calculated. Will be ignored if arc, timingFunction, or duration is set.
  343. - Parameters:
  344. - stiffness: stiffness of the spring
  345. - damping: damping of the spring
  346. */
  347. @available(iOS 9, *)
  348. public static func spring(stiffness: CGFloat, damping: CGFloat) -> HeroModifier {
  349. return HeroModifier { targetState in
  350. targetState.spring = (stiffness, damping)
  351. }
  352. }
  353. }
  354. // other modifiers
  355. extension HeroModifier {
  356. /**
  357. Transition from/to the state of the view with matching heroID
  358. Will also force the view to use global coordinate space.
  359. The following layer properties will be animated from the given view.
  360. position
  361. bounds.size
  362. cornerRadius
  363. transform
  364. shadowColor
  365. shadowOpacity
  366. shadowOffset
  367. shadowRadius
  368. shadowPath
  369. Note that the following properties **won't** be taken from the source view.
  370. backgroundColor
  371. borderWidth
  372. borderColor
  373. - Parameters:
  374. - heroID: the source view's heroId.
  375. */
  376. public static func source(heroID: String) -> HeroModifier {
  377. return HeroModifier { targetState in
  378. targetState.source = heroID
  379. }
  380. }
  381. /**
  382. Works in combination with position modifier to apply a natural curve when moving to the destination.
  383. */
  384. public static var arc: HeroModifier = .arc()
  385. /**
  386. Works in combination with position modifier to apply a natural curve when moving to the destination.
  387. - Parameters:
  388. - intensity: a value of 1 represent a downward natural curve . a value of -1 represent a upward curve .
  389. default is 1.
  390. */
  391. public static func arc(intensity: CGFloat = 1) -> HeroModifier {
  392. return HeroModifier { targetState in
  393. targetState.arc = intensity
  394. }
  395. }
  396. /**
  397. Cascade applys increasing delay modifiers to subviews
  398. */
  399. public static var cascade: HeroModifier = .cascade()
  400. /**
  401. Cascade applys increasing delay modifiers to subviews
  402. - Parameters:
  403. - delta: delay in between each animation
  404. - direction: cascade direction
  405. - delayMatchedViews: whether or not to delay matched subviews until all cascading animation have started
  406. */
  407. public static func cascade(delta: TimeInterval = 0.02,
  408. direction: CascadeDirection = .topToBottom,
  409. delayMatchedViews: Bool = false) -> HeroModifier {
  410. return HeroModifier { targetState in
  411. targetState.cascade = (delta, direction, delayMatchedViews)
  412. }
  413. }
  414. }
  415. // conditional modifiers
  416. extension HeroModifier {
  417. /**
  418. Apply modifiers only if the condition return true.
  419. */
  420. public static func when(_ condition: @escaping (HeroConditionalContext) -> Bool, _ modifiers: [HeroModifier]) -> HeroModifier {
  421. return HeroModifier { targetState in
  422. if targetState.conditionalModifiers == nil {
  423. targetState.conditionalModifiers = []
  424. }
  425. targetState.conditionalModifiers!.append((condition, modifiers))
  426. }
  427. }
  428. public static func when(_ condition: @escaping (HeroConditionalContext) -> Bool, _ modifiers: HeroModifier...) -> HeroModifier {
  429. return .when(condition, modifiers)
  430. }
  431. public static func whenMatched(_ modifiers: HeroModifier...) -> HeroModifier {
  432. return .when({ $0.isMatched }, modifiers)
  433. }
  434. public static func whenPresenting(_ modifiers: HeroModifier...) -> HeroModifier {
  435. return .when({ $0.isPresenting }, modifiers)
  436. }
  437. public static func whenDismissing(_ modifiers: HeroModifier...) -> HeroModifier {
  438. return .when({ !$0.isPresenting }, modifiers)
  439. }
  440. public static func whenAppearing(_ modifiers: HeroModifier...) -> HeroModifier {
  441. return .when({ $0.isAppearing }, modifiers)
  442. }
  443. public static func whenDisappearing(_ modifiers: HeroModifier...) -> HeroModifier {
  444. return .when({ !$0.isAppearing }, modifiers)
  445. }
  446. }