BKShiftingView.m 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // BKShiftingView.m
  3. // BKPasscodeViewDemo
  4. //
  5. // Created by Byungkook Jang on 2014. 10. 11..
  6. // Copyright (c) 2014년 Byungkook Jang. All rights reserved.
  7. //
  8. #import "BKShiftingView.h"
  9. @implementation BKShiftingView
  10. - (void)layoutSubviews
  11. {
  12. [super layoutSubviews];
  13. self.currentView.frame = self.bounds;
  14. }
  15. - (void)setCurrentView:(UIView *)currentView
  16. {
  17. if (_currentView == currentView) {
  18. return;
  19. }
  20. [_currentView removeFromSuperview];
  21. _currentView = currentView;
  22. if (currentView) {
  23. [self addSubview:currentView];
  24. }
  25. [self setNeedsLayout];
  26. }
  27. - (void)showView:(UIView *)view withDirection:(BKShiftingDirection)direction
  28. {
  29. UIView *oldView = self.currentView;
  30. oldView.userInteractionEnabled = NO;
  31. CGRect nextFrame = self.bounds;
  32. switch (direction) {
  33. case BKShiftingDirectionForward:
  34. nextFrame.origin.x = CGRectGetWidth(self.bounds);
  35. break;
  36. case BKShiftingDirectionBackward:
  37. nextFrame.origin.x = -CGRectGetWidth(self.bounds);
  38. break;
  39. }
  40. view.frame = nextFrame;
  41. [self addSubview:view];
  42. // start animation
  43. [UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
  44. switch (direction) {
  45. case BKShiftingDirectionForward:
  46. oldView.frame = CGRectOffset(oldView.frame, -CGRectGetWidth(self.bounds), 0);
  47. view.frame = CGRectOffset(view.frame, -CGRectGetWidth(self.bounds), 0);
  48. break;
  49. case BKShiftingDirectionBackward:
  50. oldView.frame = CGRectOffset(oldView.frame, CGRectGetWidth(self.bounds), 0);
  51. view.frame = CGRectOffset(view.frame, CGRectGetWidth(self.bounds), 0);
  52. break;
  53. }
  54. } completion:^(BOOL finished) {
  55. [oldView removeFromSuperview];
  56. }];
  57. _currentView = view;
  58. }
  59. @end