UIScrollView+YYAdd.m 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // UIScrollView+YYAdd.m
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 13/4/5.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import "UIScrollView+YYAdd.h"
  12. #import "YYKitMacro.h"
  13. YYSYNTH_DUMMY_CLASS(UIScrollView_YYAdd)
  14. @implementation UIScrollView (YYAdd)
  15. - (void)scrollToTop {
  16. [self scrollToTopAnimated:YES];
  17. }
  18. - (void)scrollToBottom {
  19. [self scrollToBottomAnimated:YES];
  20. }
  21. - (void)scrollToLeft {
  22. [self scrollToLeftAnimated:YES];
  23. }
  24. - (void)scrollToRight {
  25. [self scrollToRightAnimated:YES];
  26. }
  27. - (void)scrollToTopAnimated:(BOOL)animated {
  28. CGPoint off = self.contentOffset;
  29. off.y = 0 - self.contentInset.top;
  30. [self setContentOffset:off animated:animated];
  31. }
  32. - (void)scrollToBottomAnimated:(BOOL)animated {
  33. CGPoint off = self.contentOffset;
  34. off.y = self.contentSize.height - self.bounds.size.height + self.contentInset.bottom;
  35. [self setContentOffset:off animated:animated];
  36. }
  37. - (void)scrollToLeftAnimated:(BOOL)animated {
  38. CGPoint off = self.contentOffset;
  39. off.x = 0 - self.contentInset.left;
  40. [self setContentOffset:off animated:animated];
  41. }
  42. - (void)scrollToRightAnimated:(BOOL)animated {
  43. CGPoint off = self.contentOffset;
  44. off.x = self.contentSize.width - self.bounds.size.width + self.contentInset.right;
  45. [self setContentOffset:off animated:animated];
  46. }
  47. @end