UIView+SLKAdditions.m 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // SlackTextViewController
  3. // https://github.com/slackhq/SlackTextViewController
  4. //
  5. // Copyright 2014-2016 Slack Technologies, Inc.
  6. // Licence: MIT-Licence
  7. //
  8. #import "UIView+SLKAdditions.h"
  9. #import "SLKUIConstants.h"
  10. @implementation UIView (SLKAdditions)
  11. - (void)slk_animateLayoutIfNeededWithBounce:(BOOL)bounce options:(UIViewAnimationOptions)options animations:(void (^)(void))animations
  12. {
  13. [self slk_animateLayoutIfNeededWithBounce:bounce options:options animations:animations completion:NULL];
  14. }
  15. - (void)slk_animateLayoutIfNeededWithBounce:(BOOL)bounce options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
  16. {
  17. NSTimeInterval duration = bounce ? 0.65 : 0.2;
  18. [self slk_animateLayoutIfNeededWithDuration:duration bounce:bounce options:options animations:animations completion:completion];
  19. }
  20. - (void)slk_animateLayoutIfNeededWithDuration:(NSTimeInterval)duration bounce:(BOOL)bounce options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
  21. {
  22. if (bounce) {
  23. [UIView animateWithDuration:duration
  24. delay:0.0
  25. usingSpringWithDamping:0.7
  26. initialSpringVelocity:0.7
  27. options:options
  28. animations:^{
  29. [self layoutIfNeeded];
  30. if (animations) {
  31. animations();
  32. }
  33. }
  34. completion:completion];
  35. }
  36. else {
  37. [UIView animateWithDuration:duration
  38. delay:0.0
  39. options:options
  40. animations:^{
  41. [self layoutIfNeeded];
  42. if (animations) {
  43. animations();
  44. }
  45. }
  46. completion:completion];
  47. }
  48. }
  49. - (NSArray *)slk_constraintsForAttribute:(NSLayoutAttribute)attribute
  50. {
  51. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstAttribute = %d", attribute];
  52. return [self.constraints filteredArrayUsingPredicate:predicate];
  53. }
  54. - (NSLayoutConstraint *)slk_constraintForAttribute:(NSLayoutAttribute)attribute firstItem:(id)first secondItem:(id)second
  55. {
  56. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstAttribute = %d AND firstItem = %@ AND secondItem = %@", attribute, first, second];
  57. return [[self.constraints filteredArrayUsingPredicate:predicate] firstObject];
  58. }
  59. @end