.h file
@interface ViewController : UIViewController {
UIImageView *_topBackground;
UIButton *_leftArrow, *_rightArrow;
UILabel *_dateLabel;
IBOutlet UILabel *result;
NSDate *day1;
}
.m file
#import <QuartzCore/QuartzCore.h>
static NSString *TOP_BACKGROUND_IMAGE = @"ma_topBackground.png";
static NSString *LEFT_ARROW_IMAGE = @"ma_leftArrow.png";
static NSString *RIGHT_ARROW_IMAGE = @"ma_rightArrow.png";
static const unsigned int ARROW_LEFT = 100;
static const unsigned int ARROW_RIGHT = 101;
static const unsigned int ARROW_WIDTH = 48;
static const unsigned int ARROW_HEIGHT = 38;
static const unsigned int TOP_BACKGROUND_HEIGHT = 95;
#define CURRENT_CALENDAR [NSCalendar currentCalendar]
#define DATE_COMPONENTS (NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit)
@interface ViewController()
- (void)changeDay:(UIButton *)sender;
- (NSDate *)nextDayFromDate:(NSDate *)date;
- (NSDate *)previousDayFromDate:(NSDate *)date;
@property (readonly) UIImageView *topBackground;
@property (readonly) UIButton *leftArrow;
@property (readonly) UIButton *rightArrow;
@property (readonly) UILabel *dateLabel;
@property (readonly) UIFont *regularFont;
@property (readonly) UIFont *boldFont;
@property (readonly) NSString *titleText;
@end
@implementation ViewController
- (UIImageView *)topBackground {
if (!_topBackground) {
_topBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:TOP_BACKGROUND_IMAGE]];
}
return _topBackground;
}
- (UIButton *)leftArrow {
if (!_leftArrow) {
_leftArrow = [UIButton buttonWithType:UIButtonTypeCustom];
_leftArrow.tag = ARROW_LEFT;
[_leftArrow setImage:[UIImage imageNamed:LEFT_ARROW_IMAGE] forState:0];
[_leftArrow addTarget:self action:@selector(changeDay:) forControlEvents:UIControlEventTouchUpInside];
}
return _leftArrow;
}
- (UIButton *)rightArrow {
if (!_rightArrow) {
_rightArrow = [UIButton buttonWithType:UIButtonTypeCustom];
_rightArrow.tag = ARROW_RIGHT;
[_rightArrow setImage:[UIImage imageNamed:RIGHT_ARROW_IMAGE] forState:0];
[_rightArrow addTarget:self action:@selector(changeDay:) forControlEvents:UIControlEventTouchUpInside];
}
return _rightArrow;
}
- (UILabel *)dateLabel {
if (!_dateLabel) {
_dateLabel = [[UILabel alloc] init];
_dateLabel.textAlignment = NSTextAlignmentCenter;
_dateLabel.backgroundColor = [UIColor clearColor];
_dateLabel.font = [UIFont boldSystemFontOfSize:18];
_dateLabel.textColor = [UIColor colorWithRed:59/255. green:73/255. blue:88/255. alpha:1];
}
return _dateLabel;
}
- (void)setDay:(NSDate *)date {
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:date];
day1 = [CURRENT_CALENDAR dateFromComponents:components];
[day1 copy];
self.dateLabel.text = [self titleText];
NSString *str = self.dateLabel.text;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
NSDate *date1 = [dateFormatter dateFromString: str];
dateFormatter = [[NSDateFormatter alloc] init] ;
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *convertedString = [dateFormatter stringFromDate:date1];
result.text = convertedString;
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(@"datestring %@",dateString);
NSLog(@"cur1: %@",self.dateLabel.text);
}
- (void)changeDay:(UIButton *)sender {
if (ARROW_LEFT == sender.tag) {
self.day = [self previousDayFromDate:day1];
} else if (ARROW_RIGHT == sender.tag) {
self.day = [self nextDayFromDate:day1];
}
}
- (NSDate *)nextDayFromDate:(NSDate *)date {
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:date];
[components setDay:[components day] + 1];
return [CURRENT_CALENDAR dateFromComponents:components];
}
- (NSDate *)previousDayFromDate:(NSDate *)date {
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(@"datestring %@",dateString);
NSLog(@"cur1: %@",self.dateLabel.text);
if (dateString == self.dateLabel.text) {
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:date];
[components setDay:[components day]];
return [CURRENT_CALENDAR dateFromComponents:components];
}else{
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:date];
[components setDay:[components day] - 1];
return [CURRENT_CALENDAR dateFromComponents:components];
}
}
- (NSString *)titleText {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy"];
return [NSString stringWithFormat:@"%@",
[formatter stringFromDate:day1]];
}
- (void)viewDidLoad
{
self.rightArrow.frame = CGRectMake(280, 60, 20, 20);
[self.view addSubview:_rightArrow];
self.leftArrow.frame = CGRectMake(10, 60, 20, 20);
[self.view addSubview:_leftArrow];
self.day = [NSDate date];
[self.view addSubview:_dateLabel];
self.dateLabel.frame = CGRectMake(60, 0, 200, 150);
self.topBackground.frame = CGRectMake(0, 0, 320, 45);
[self.view addSubview:_topBackground];
[super viewDidLoad];
}
@end
No comments:
Post a Comment