Advertisement
thieumao

Temp

Aug 23rd, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  FCalendarView.m
  3. //  TNUSocial
  4. //
  5. //  Created by Thieu Mao on 8/21/16.
  6. //  Copyright © 2016 OTVINA. All rights reserved.
  7. //
  8.  
  9. #import "FCalendarView.h"
  10. #import "FDateUtility.h"
  11. #import "WeekView.h"
  12.  
  13. @interface FCalendarView ()
  14. @property (strong, nonatomic) FMonthView *monthView;
  15. @property (strong, nonatomic) FMonthView *monthViewNextMonth;
  16. @property (strong, nonatomic) FMonthView *monthViewPreViousMonth;
  17. @property (strong, nonatomic) UIButton *btnPreviousMonth;
  18. @property (strong, nonatomic) UIButton *btnNextMonth;
  19. @property (strong, nonatomic) WeekView *weekView; // ngay trong tuan
  20. @property (strong, nonatomic) UIView *infoView; // view chua
  21. @property (strong, nonatomic) UIView *containerView;
  22. @property (strong, nonatomic) UILabel *lbInfor; // thang
  23. @property (strong, nonatomic) NSDateFormatter *formatter;
  24. @property (strong, nonatomic) UISwipeGestureRecognizer *swipeLeft;
  25. @property (strong, nonatomic) UISwipeGestureRecognizer *swipeRight;
  26. @property (strong, nonatomic) NSMutableArray *monthViews;
  27. @property (copy, nonatomic) void(^callBackBlock)(NSDate *dateSelected,NSInteger swapView);
  28. @end
  29.  
  30. @implementation FCalendarView
  31. #pragma mark - Init
  32. - (id)initWithFrame:(CGRect)frame calendar:(NSCalendar *)calendar monthDisplay:(NSDate *)month
  33.     dateSelected:(NSDate *)date showDayOff:(BOOL)showDayOff dayViewHeight:(CGFloat)height {
  34.    
  35.     self = [super initWithFrame:frame];
  36.     if (self) {
  37.         //Set data
  38.         self.calendar = calendar;
  39.         self.monthDisplay = month;
  40.         self.dateSelected = date;
  41.         self.showDayOff = showDayOff;
  42.         self.dayViewHeight = height;
  43.         [self setBackgroundColor:[UIColor whiteColor]];
  44.         if (!self.calendar) {
  45.             self.calendar = [NSCalendar currentCalendar];
  46.             self.calendar.firstWeekday = 2;
  47.         }
  48.         if (!self.monthDisplay) {
  49.             self.monthDisplay = [NSDate date];
  50.         }
  51.         if (self.dateSelected) {
  52.             self.monthDisplay = self.dateSelected;
  53.             ((FCalendarSingleton*)[FCalendarSingleton sharedInstance]).dateSelected = self.dateSelected;
  54.         }
  55.         //Create Date formatter and label
  56.         self.formatter = [[NSDateFormatter alloc] init];
  57.         [self.formatter setDateFormat:@"MMMM yyyy"];
  58.         self.lbInfor = [[UILabel alloc] init];
  59.         [self.lbInfor setTextAlignment:NSTextAlignmentCenter];
  60.     }
  61.     return self;
  62. }
  63.  
  64. - (void)setDataWithCalendar:(NSCalendar *)calendar monthDisplay:(NSDate *)month dateSelected:(NSDate *)date
  65.     showDayOff:(BOOL)showDayOff dayViewHeight:(CGFloat)height {
  66.     self.monthViews = [[NSMutableArray alloc] init];
  67.     //Set data
  68.     self.calendar = calendar;
  69.     self.monthDisplay = month;
  70.     self.dateSelected = date;
  71.     self.showDayOff = showDayOff;
  72.     self.dayViewHeight = height;
  73.     [self setBackgroundColor:[UIColor whiteColor]];
  74.     if (!self.calendar) {
  75.         self.calendar = [NSCalendar currentCalendar];
  76.         // chọn ngày bắt đầu: =8 có nghĩa là  CN T2 T3 ... T7
  77.         self.calendar.firstWeekday = 8;
  78.     }
  79.     if (!self.monthDisplay) {
  80.         self.monthDisplay = [NSDate date];
  81.     }
  82.     if (self.dateSelected) {
  83.         self.monthDisplay = self.dateSelected;
  84.         ((FCalendarSingleton *)[FCalendarSingleton sharedInstance]).dateSelected = self.dateSelected;
  85.     }
  86.     //Create Date formatter and label
  87.     self.formatter = [[NSDateFormatter alloc] init];
  88.     [self.formatter setDateFormat:@"MMMM yyyy"];
  89.     self.lbInfor = [[UILabel alloc] init];
  90.     [self.lbInfor setTextAlignment:NSTextAlignmentCenter];
  91.     [self createUIWithFrame:self.bounds];
  92. }
  93.  
  94. - (void)createUIWithFrame:(CGRect)frame {
  95.     //Create Month Label
  96.     self.infoView = [[UIView alloc] init];
  97.     [self.infoView setTranslatesAutoresizingMaskIntoConstraints:NO];
  98.     [self.infoView setBackgroundColor:[UIColor yellowColor]];
  99.     [self addSubview:self.infoView];
  100.     //Constraint InfoView
  101.     [self addConstraint:[NSLayoutConstraint constraintWithItem:self.infoView attribute:NSLayoutAttributeWidth
  102.         relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
  103.     [self addConstraint:[NSLayoutConstraint constraintWithItem:self.infoView attribute:NSLayoutAttributeHeight
  104.         relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1
  105.             constant:INFOVIEW_HEIGHT]];
  106.     [self addConstraint:[NSLayoutConstraint constraintWithItem:self.infoView attribute:NSLayoutAttributeTop
  107.         relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
  108.     [self addConstraint:[NSLayoutConstraint constraintWithItem:self.infoView attribute:NSLayoutAttributeLeading
  109.         relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]];
  110.     [self.infoView addSubview:self.lbInfor];
  111.     [self.lbInfor setTranslatesAutoresizingMaskIntoConstraints:NO];
  112.     [self.infoView addConstraint:[NSLayoutConstraint constraintWithItem:self.lbInfor attribute:NSLayoutAttributeTop
  113.         relatedBy:NSLayoutRelationEqual toItem:self.infoView attribute:NSLayoutAttributeTop multiplier:1.0
  114.             constant:0.0]];
  115.     [self.infoView addConstraint:[NSLayoutConstraint constraintWithItem:self.lbInfor attribute:NSLayoutAttributeBottom
  116.         relatedBy:NSLayoutRelationEqual toItem:self.infoView attribute:NSLayoutAttributeBottom multiplier:1.0
  117.             constant:0.0]];
  118.     [self.infoView addConstraint:[NSLayoutConstraint constraintWithItem:self.lbInfor attribute:NSLayoutAttributeLeading
  119.         relatedBy:NSLayoutRelationEqual toItem:self.infoView attribute:NSLayoutAttributeLeading multiplier:1.0
  120.             constant:0.0]];
  121.     [self.infoView addConstraint:[NSLayoutConstraint constraintWithItem:self.lbInfor attribute:NSLayoutAttributeTrailing
  122.         relatedBy:NSLayoutRelationEqual toItem:self.infoView attribute:NSLayoutAttributeTrailing multiplier:1.0
  123.             constant:0.0]];
  124.     //Create 2 button to change month
  125.     //Button next month
  126.     self.btnPreviousMonth = [[UIButton alloc] init];
  127.     [self.btnPreviousMonth setTitle:@"<" forState:UIControlStateNormal];
  128.     [self.btnPreviousMonth setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
  129.     [self.btnPreviousMonth addTarget:self action:@selector(moveToPreviousMonth)
  130.         forControlEvents:UIControlEventTouchUpInside];
  131.     //Button previous month
  132.     self.btnNextMonth = [[UIButton alloc] init];
  133.     [self.btnNextMonth setTitle:@">" forState:UIControlStateNormal];
  134.     [self.btnNextMonth setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
  135.     [self.btnNextMonth addTarget:self action:@selector(moveToNextMonth) forControlEvents:UIControlEventTouchUpInside];
  136.     //Add 2 buttons
  137.     [self.infoView addSubview:self.btnNextMonth];
  138.     [self.infoView addSubview:self.btnPreviousMonth];
  139.     [self.btnNextMonth setTranslatesAutoresizingMaskIntoConstraints:NO];
  140.     [self.btnPreviousMonth setTranslatesAutoresizingMaskIntoConstraints:NO];
  141.     [self.infoView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnPreviousMonth
  142.         attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.infoView
  143.             attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
  144.     [self.infoView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnPreviousMonth
  145.         attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.infoView
  146.             attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
  147.     [self.infoView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnPreviousMonth
  148.         attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.infoView
  149.             attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]];
  150.     [self.btnPreviousMonth addConstraint:[NSLayoutConstraint constraintWithItem:self.btnPreviousMonth
  151.         attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.btnPreviousMonth
  152.             attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.0]];
  153.     [self.btnNextMonth setTranslatesAutoresizingMaskIntoConstraints:NO];
  154.     [self.infoView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnNextMonth attribute:NSLayoutAttributeTop
  155.         relatedBy:NSLayoutRelationEqual toItem:self.infoView attribute:NSLayoutAttributeTop multiplier:1.0
  156.             constant:0.0]];
  157.     [self.infoView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnNextMonth
  158.         attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.infoView
  159.             attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
  160.     [self.infoView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnNextMonth
  161.         attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.infoView
  162.             attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]];
  163.     [self.btnNextMonth addConstraint:[NSLayoutConstraint constraintWithItem:self.btnNextMonth
  164.         attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.btnNextMonth
  165.             attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0.0]];
  166.     //Create WeekView
  167.     self.weekView = [[WeekView alloc] init];
  168.     [self.weekView setCurrentCalendar:self.calendar];
  169.     [self addSubview:self.weekView];
  170.     [self.weekView setTranslatesAutoresizingMaskIntoConstraints:NO];
  171.     [self addConstraint:[NSLayoutConstraint constraintWithItem:self.weekView attribute:NSLayoutAttributeTop
  172.         relatedBy:NSLayoutRelationEqual toItem:self.infoView attribute:NSLayoutAttributeBottom multiplier:1.0
  173.             constant:0.0]];
  174.     [self addConstraint:[NSLayoutConstraint constraintWithItem:self.weekView attribute:NSLayoutAttributeLeading
  175.         relatedBy:NSLayoutRelationEqual toItem:self.infoView attribute:NSLayoutAttributeLeading multiplier:1.0
  176.             constant:0.0]];
  177.     [self addConstraint:[NSLayoutConstraint constraintWithItem:self.weekView attribute:NSLayoutAttributeTrailing
  178.         relatedBy:NSLayoutRelationEqual toItem:self.infoView attribute:NSLayoutAttributeTrailing multiplier:1.0
  179.             constant:0.0]];
  180.     [self.weekView addConstraint:[NSLayoutConstraint constraintWithItem:self.weekView attribute:NSLayoutAttributeHeight
  181.         relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0
  182.             constant:WEEKVIEW_HEIGHT]];
  183.     //Create main calendar
  184.     [self addCalendarView];
  185. }
  186.  
  187. - (void)addCalendarView {
  188.     self.lbInfor.text = [self.formatter stringFromDate:self.monthDisplay];
  189.     //Swipe Recognize
  190.     self.swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
  191.     self.swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
  192.     // Setting the swipe direction.
  193.     [self.swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
  194.     [self.swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
  195.     //Pan Recognizer
  196.     UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
  197.         action:@selector(handelPan:)];
  198.     [panRecognizer setMaximumNumberOfTouches:1];
  199.     [self addGestureRecognizer:panRecognizer];
  200.     //create block
  201.     __weak typeof(self) weakSelf = self;
  202.     self.callBackBlock = ^(NSDate *date, NSInteger swapIndex){
  203.         if (swapIndex == 0) {
  204.             [weakSelf handleSwipe:weakSelf.swipeRight];
  205.         } else if (swapIndex == 2) {
  206.             [weakSelf handleSwipe:weakSelf.swipeLeft];
  207.         }
  208.         if (weakSelf.delegate) {
  209.             [weakSelf.delegate didSelectDate:date];
  210.         }
  211.     };
  212.     CGRect frame = self.containerView.bounds;
  213.     //create containerView
  214.     self.containerView = [[UIView alloc] init];
  215.     [self.containerView setBackgroundColor:[UIColor whiteColor]];
  216.     [self addSubview:self.containerView];
  217.     [self.containerView setTranslatesAutoresizingMaskIntoConstraints:NO];
  218.     [self addConstraint:[NSLayoutConstraint constraintWithItem:self.containerView attribute:NSLayoutAttributeTop
  219.         relatedBy:NSLayoutRelationEqual toItem:self.weekView attribute:NSLayoutAttributeBottom multiplier:1.0
  220.             constant:0.0]];
  221.     [self addConstraint:[NSLayoutConstraint constraintWithItem:self.containerView attribute:NSLayoutAttributeLeading
  222.         relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]];
  223.     [self addConstraint:[NSLayoutConstraint constraintWithItem:self.containerView attribute:NSLayoutAttributeTrailing
  224.         relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]];
  225.     [self addConstraint:[NSLayoutConstraint constraintWithItem:self.containerView attribute:NSLayoutAttributeBottom
  226.         relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
  227.     //Create 3 Monthview for reuse
  228. //    for (NSUInteger i=0; i<3; i++) {
  229. //        FMonthView *monthView = [[FMonthView alloc] initWithCalendar:self.calendar showDayOff:self.showDayOff
  230. //            dayViewHeight:self.dayViewHeight];
  231. //        switch (i) {
  232. //            case 0:
  233. //                monthView.month = [self.monthDisplay previousMonth];
  234. //                frame.origin.x = -(frame.size.width);
  235. //                break;
  236. //            case 1:
  237. //                monthView.month = [self.monthDisplay nextMonth];
  238. //                frame.origin.x = frame.size.width;
  239. //                break;
  240. //            case 2:
  241. //                monthView.month = self.monthDisplay;
  242. //                frame.origin.x = 0;
  243. //                break;
  244. //            default:
  245. //                break;
  246. //        }
  247. //        //Layout container view
  248. //        [self.containerView addSubview:monthView];
  249. //        [monthView setTranslatesAutoresizingMaskIntoConstraints:NO];
  250. //        [self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:monthView
  251. //            attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.containerView
  252. //                attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
  253. //        [self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:monthView
  254. //            attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.containerView
  255. //                attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
  256. //        [self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:monthView
  257. //            attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.containerView
  258. //                attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]];
  259. //        [self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:monthView
  260. //            attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.containerView
  261. //                attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]];
  262. //        monthView.frame = frame;
  263. //        [self.containerView layoutSubviews];
  264. //        [monthView createView];
  265. //        [self.monthViews addObject:monthView];
  266. //        [monthView didSelectDateWithCompletion:^(NSDate *dateSelected, NSInteger swapView) {
  267. //            [self reloadMonthView];
  268. //            if (swapView == 0) {
  269. //                [self moveToPreviousMonth];
  270. //            }
  271. //            if (swapView == 2) {
  272. //                [self moveToNextMonth];
  273. //            }
  274. //            if ([self.delegate respondsToSelector:@selector(didSelectDate:)]) {
  275. //                [self.delegate didSelectDate:dateSelected];
  276. //            }
  277. //        }];
  278. //    }
  279. #pragma mark - i = 0
  280.     FMonthView *monthView0 = [[FMonthView alloc] initWithCalendar:self.calendar showDayOff:self.showDayOff
  281.                                                    dayViewHeight:self.dayViewHeight];
  282. //    switch (i) {
  283. //        case 0:
  284.             monthView0.month = [self.monthDisplay previousMonth];
  285.             frame.origin.x = -(frame.size.width);
  286. //            break;
  287. //        case 1:
  288. //            monthView.month = [self.monthDisplay nextMonth];
  289. //            frame.origin.x = frame.size.width;
  290. //            break;
  291. //        case 2:
  292. //            monthView.month = self.monthDisplay;
  293. //            frame.origin.x = 0;
  294. //            break;
  295. //        default:
  296. //            break;
  297. //    }
  298.     //Layout container view
  299.     [self.containerView addSubview:monthView0];
  300.     [monthView0 setTranslatesAutoresizingMaskIntoConstraints:NO];
  301.     [self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:monthView0
  302.                                                                    attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.containerView
  303.                                                                    attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
  304.     [self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:monthView0
  305.                                                                    attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.containerView
  306.                                                                    attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
  307.     [self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:monthView0
  308.                                                                    attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.containerView
  309.                                                                    attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]];
  310.     [self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:monthView0
  311.                                                                    attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.containerView
  312.                                                                    attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]];
  313.     monthView0.frame = frame;
  314.     [self.containerView layoutSubviews];
  315.     [monthView0 createView];
  316. //    [self.monthViews addObject:monthView0];
  317.     [monthView0 didSelectDateWithCompletion:^(NSDate *dateSelected, NSInteger swapView) {
  318.         [self reloadMonthView];
  319.         if (swapView == 0) {
  320.             [self moveToPreviousMonth];
  321.         }
  322.         if (swapView == 2) {
  323.             [self moveToNextMonth];
  324.         }
  325.         if ([self.delegate respondsToSelector:@selector(didSelectDate:)]) {
  326.             [self.delegate didSelectDate:dateSelected];
  327.         }
  328.     }];
  329. #pragma mark - i = 1
  330.     FMonthView *monthView1 = [[FMonthView alloc] initWithCalendar:self.calendar showDayOff:self.showDayOff
  331.                                                    dayViewHeight:self.dayViewHeight];
  332. //    switch (i) {
  333. //        case 0:
  334. //            monthView.month = [self.monthDisplay previousMonth];
  335. //            frame.origin.x = -(frame.size.width);
  336. //            break;
  337. //        case 1:
  338.             monthView1.month = [self.monthDisplay nextMonth];
  339.             frame.origin.x = frame.size.width;
  340. //            break;
  341. //        case 2:
  342. //            monthView.month = self.monthDisplay;
  343. //            frame.origin.x = 0;
  344. //            break;
  345. //        default:
  346. //            break;
  347. //    }
  348.     //Layout container view
  349.     [self.containerView addSubview:monthView1];
  350.     [monthView1 setTranslatesAutoresizingMaskIntoConstraints:NO];
  351.     [self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:monthView1
  352.                                                                    attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.containerView
  353.                                                                    attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
  354.     [self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:monthView1
  355.                                                                    attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.containerView
  356.                                                                    attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
  357.     [self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:monthView1
  358.                                                                    attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.containerView
  359.                                                                    attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]];
  360.     [self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:monthView1
  361.                                                                    attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.containerView
  362.                                                                    attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]];
  363.     monthView1.frame = frame;
  364.     [self.containerView layoutSubviews];
  365.     [monthView1 createView];
  366. //    [self.monthViews addObject:monthView1];
  367.     [monthView1 didSelectDateWithCompletion:^(NSDate *dateSelected, NSInteger swapView) {
  368.         [self reloadMonthView];
  369.         if (swapView == 0) {
  370.             [self moveToPreviousMonth];
  371.         }
  372.         if (swapView == 2) {
  373.             [self moveToNextMonth];
  374.         }
  375.         if ([self.delegate respondsToSelector:@selector(didSelectDate:)]) {
  376.             [self.delegate didSelectDate:dateSelected];
  377.         }
  378.     }];
  379. #pragma mark - i = 2
  380.     FMonthView *monthView2 = [[FMonthView alloc] initWithCalendar:self.calendar showDayOff:self.showDayOff
  381.                                                    dayViewHeight:self.dayViewHeight];
  382. //    switch (i) {
  383. //        case 0:
  384. //            monthView.month = [self.monthDisplay previousMonth];
  385. //            frame.origin.x = -(frame.size.width);
  386. //            break;
  387. //        case 1:
  388. //            monthView1.month = [self.monthDisplay nextMonth];
  389. //            frame.origin.x = frame.size.width;
  390. //            break;
  391. //        case 2:
  392.             monthView2.month = self.monthDisplay;
  393.             frame.origin.x = 0;
  394. //            break;
  395. //        default:
  396. //            break;
  397. //    }
  398.     //Layout container view
  399.     [self.containerView addSubview:monthView2];
  400.     [monthView2 setTranslatesAutoresizingMaskIntoConstraints:NO];
  401.     [self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:monthView2
  402.                                                                    attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.containerView
  403.                                                                    attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
  404.     [self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:monthView2
  405.                                                                    attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.containerView
  406.                                                                    attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
  407.     [self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:monthView2
  408.                                                                    attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.containerView
  409.                                                                    attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]];
  410.     [self.containerView addConstraint:[NSLayoutConstraint constraintWithItem:monthView2
  411.                                                                    attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.containerView
  412.                                                                    attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]];
  413.     monthView2.frame = frame;
  414.     [self.containerView layoutSubviews];
  415.     [monthView2 createView];
  416. //    [self.monthViews addObject:monthView2];
  417.     [monthView2 didSelectDateWithCompletion:^(NSDate *dateSelected, NSInteger swapView) {
  418.         [self reloadMonthView];
  419.         if (swapView == 0) {
  420.             [self moveToPreviousMonth];
  421.         }
  422.         if (swapView == 2) {
  423.             [self moveToNextMonth];
  424.         }
  425.         if ([self.delegate respondsToSelector:@selector(didSelectDate:)]) {
  426.             [self.delegate didSelectDate:dateSelected];
  427.         }
  428.     }];
  429. //    self.monthViews = @[monthView0, monthView1, monthView2];
  430.     [self.monthViews addObject:monthView0];
  431.     [self.monthViews addObject:monthView2];
  432.     [self.monthViews addObject:monthView1];
  433.     [self setMonthViewPositionWithTranslate:CGPointMake(0, 0)];
  434.     self.monthView = ((FMonthView *)[self.monthViews objectAtIndex:1]);
  435. }
  436.  
  437. #pragma mark - Swipe handler
  438. - (void)handelPan:(UIPanGestureRecognizer *)recognizer {
  439.     CGPoint translate = [recognizer translationInView:self];
  440.     [self setMonthViewPositionWithTranslate:translate];
  441.     if (recognizer.state == UIGestureRecognizerStateEnded) {
  442.         if (self.monthView.frame.origin.x > (self.bounds.size.width / 3)
  443.             || ([recognizer velocityInView:self].x > 600.0)) {
  444.             [self handleSwipe:self.swipeRight];
  445.         } else if (self.monthView.frame.origin.x < -(self.bounds.size.width / 3)
  446.             || ([recognizer velocityInView:self].x < (-600.0))) {
  447.             [self handleSwipe:self.swipeLeft];
  448.         } else {
  449.             [UIView animateWithDuration:0.2 animations:^{
  450.                 [self setMonthViewPositionWithTranslate:CGPointMake(0, 0)];
  451.             }];
  452.         }
  453.     }
  454. }
  455.  
  456. -(void)handleSwipe:(UISwipeGestureRecognizer *)recognizer {
  457.     NSUInteger index;
  458.     FMonthView *monthViewRemain;
  459.     CGRect firstFrame;
  460.     CGRect secondFrame;
  461.     CGRect monthViewRemainFrame;
  462.     if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
  463.         self.monthDisplay = [self.monthDisplay previousMonth];
  464.         index = 0;
  465.         monthViewRemain = [self.monthViews objectAtIndex:2];
  466.         monthViewRemainFrame = firstFrame = CGRectMake(self.containerView.bounds.size.width, 0,
  467.             self.containerView.bounds.size.width, self.containerView.bounds.size.height);
  468.         secondFrame = CGRectMake(-self.containerView.bounds.size.width, 0, self.containerView.bounds.size.width,
  469.             self.containerView.bounds.size.height);
  470.     } else {
  471.         self.monthDisplay = [self.monthDisplay nextMonth];
  472.         index = 2;
  473.         monthViewRemain = [self.monthViews objectAtIndex:0];
  474.         monthViewRemainFrame = firstFrame = CGRectMake(-self.containerView.bounds.size.width, 0,
  475.             self.containerView.bounds.size.width, self.containerView.bounds.size.height);
  476.         secondFrame = CGRectMake(self.containerView.bounds.size.width, 0, self.containerView.bounds.size.width,
  477.             self.containerView.bounds.size.height);
  478.     }
  479.     self.lbInfor.text = [self.formatter stringFromDate:self.monthDisplay];
  480.     [UIView animateWithDuration:0.2 animations:^{
  481.         [self.btnNextMonth setUserInteractionEnabled:NO];
  482.         [self.btnPreviousMonth setUserInteractionEnabled:NO];
  483.         FMonthView *monthViewWillReplace = (FMonthView *)[self.monthViews objectAtIndex:index];
  484.         monthViewWillReplace.frame = self.containerView.bounds;
  485.         self.monthView.frame = firstFrame;
  486.         monthViewRemain.frame = monthViewRemainFrame;
  487.     } completion:^(BOOL finished) {
  488.         self.monthView.frame = secondFrame;
  489.         self.monthView = (FMonthView *)([self.monthViews objectAtIndex:index]);
  490.         [self.monthViews exchangeObjectAtIndex:index withObjectAtIndex:1];
  491.         [self reloadMonthView];
  492.         [self.btnNextMonth setUserInteractionEnabled:YES];
  493.         [self.btnPreviousMonth setUserInteractionEnabled:YES];
  494.     }];
  495.     if ([self.delegate respondsToSelector:@selector(didScrollToMonth:)]) {
  496.         // The object does implement the method, so you can safely call it.
  497.         [self.delegate didScrollToMonth:self.monthDisplay];
  498.     }
  499. }
  500.  
  501. #pragma mark - Call to change MonthView
  502. - (void)moveToNextMonth {
  503.     [self handleSwipe:self.swipeLeft];
  504. }
  505. - (void)moveToPreviousMonth {
  506.     [self handleSwipe:self.swipeRight];
  507. }
  508.  
  509. - (void)reloadMonthView {
  510.     FMonthView *preMonthView = [self.monthViews objectAtIndex:0];
  511.     FMonthView *nextMonthView = [self.monthViews objectAtIndex:2];
  512.     preMonthView.month = [self.monthDisplay previousMonth];
  513.     nextMonthView.month = [self.monthDisplay nextMonth];
  514.     [preMonthView reloadDayView];
  515.     [nextMonthView reloadDayView];
  516.     //[self setMonthViewPositionWithTranslate:CGPointMake(0, 0)];
  517. }
  518.  
  519. - (void)setMonthViewPositionWithTranslate:(CGPoint)translate {
  520.     // index 0 is first monthview
  521.     // indeo 2 is last monthview
  522.     CGRect frame = self.monthView.frame;
  523.     frame.origin.x = translate.x;
  524.     self.monthView.frame = frame;
  525.     CGRect frame0 = self.containerView.bounds;
  526.     CGRect frame2 = self.containerView.bounds;
  527.     frame0.origin.x = -(self.bounds.size.width) + translate.x;
  528.     frame2.origin.x = self.bounds.size.width + translate.x;
  529.     ((FMonthView *)[self.monthViews objectAtIndex:2]).frame = frame2;
  530.     ((FMonthView *)[self.monthViews objectAtIndex:0]).frame = frame0;
  531. }
  532.  
  533. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement