UIScrollView+SLKAdditions.m 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // SlackTextViewController
  3. // https://github.com/slackhq/SlackTextViewController
  4. //
  5. // Copyright 2014-2016 Slack Technologies, Inc.
  6. // Licence: MIT-Licence
  7. //
  8. #import "UIScrollView+SLKAdditions.h"
  9. @implementation UIScrollView (SLKAdditions)
  10. - (void)slk_scrollToTopAnimated:(BOOL)animated
  11. {
  12. if ([self slk_canScroll]) {
  13. [self setContentOffset:CGPointZero animated:animated];
  14. }
  15. }
  16. - (void)slk_scrollToBottomAnimated:(BOOL)animated
  17. {
  18. if ([self slk_canScroll]) {
  19. [self setContentOffset:[self slk_bottomRect].origin animated:animated];
  20. }
  21. }
  22. - (BOOL)slk_canScroll
  23. {
  24. if (self.contentSize.height > CGRectGetHeight(self.frame)) {
  25. return YES;
  26. }
  27. return NO;
  28. }
  29. - (BOOL)slk_isAtTop
  30. {
  31. return CGRectGetMinY([self slk_visibleRect]) <= CGRectGetMinY(self.bounds);
  32. }
  33. - (BOOL)slk_isAtBottom
  34. {
  35. return CGRectGetMaxY([self slk_visibleRect]) >= CGRectGetMaxY([self slk_bottomRect]);
  36. }
  37. - (CGRect)slk_visibleRect
  38. {
  39. CGRect visibleRect;
  40. visibleRect.origin = self.contentOffset;
  41. visibleRect.size = self.frame.size;
  42. return visibleRect;
  43. }
  44. - (CGRect)slk_bottomRect
  45. {
  46. return CGRectMake(0.0, self.contentSize.height - CGRectGetHeight(self.bounds), CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds));
  47. }
  48. - (void)slk_stopScrolling
  49. {
  50. if (!self.isDragging) {
  51. return;
  52. }
  53. CGPoint offset = self.contentOffset;
  54. offset.y -= 1.0;
  55. [self setContentOffset:offset];
  56. offset.y += 1.0;
  57. [self setContentOffset:offset];
  58. }
  59. @end