muhku / calendar-ui

MACalendarUI is a project which offers calendar user interface for iPhone applications.
Other
297 stars 90 forks source link

Detect Empty Time Slots #19

Open RizITN opened 10 years ago

RizITN commented 10 years ago

Hi, I have a requirement that user could add events against the empty slots. Whenever user select any empty slot, its corresponding time slot should be selected as StartTime, then add the other information via custom view.

So is it possible we can detect empty slots?

Thanks & Regards

SharatGuduru44 commented 10 years ago

implementing this method in MAGridView (MAWeekViewAdditions) i got the start time and end time for empty slot. iam not sure if this approach is write or wrong as i am new to ios Development.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.weekView=[MAWeekView new];
        const double posX = _touchX.x / self.cellWidth;
        const double posY = _touchX.y / self.cellHeight;

        /* Calculate the new time for the event */

//      const int eventDurationInMinutes = [self.event durationInMinutes];
        NSDate *weekday = [self.weekView.weekdayBarView.weekdays objectAtIndex:(int)round(posX)];
        double hours;
        double minutes;
        minutes = modf(posY, &hours) * 30;

        NSDateComponents *startComponents = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:weekday];
        [startComponents setHour:(int)hours];
        [startComponents setMinute:[self modifiedMinutes:minutes]];
        [startComponents setSecond:0];

        [EventManger instance].startTime = [CURRENT_CALENDAR dateFromComponents:startComponents];
        [EventManger instance].endTime   = [[EventManger instance].startTime dateByAddingTimeInterval:60];
//      self.event.displayDate = [CURRENT_CALENDAR dateFromComponents:startComponents];

        return;

    [super touchesEnded:touches withEvent:event];
}
stefanocdn commented 10 years ago

Was trying to perform same thing, but new to ios as well. Does this solution works for you?

JordanMontel commented 10 years ago

Hi, I had the same problem and I fixed this by adding a gesture recognizer to the weekView in WeekViewController.

- (void)viewDidLoad
{
    [super viewDidLoad];

    // WeekView
    self.weekView = (MAWeekView *)self.view;

    // Touch grid with gesture recognizer
    self.singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [self.singleTapGestureRecognizer setNumberOfTapsRequired:1];
    [self.weekView addGestureRecognizer:self.singleTapGestureRecognizer];
}

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
    // Point position
    CGPoint touch = [recognizer locationInView:self.weekView];
    const double posX = touch.x / self.weekView.gridView.cellWidth;
    const double posY = touch.y / self.weekView.gridView.cellHeight;

    // Current date
    NSDate *weekday = [self.weekView.weekdayBarView.weekdays objectAtIndex:(int)round(posX)];
    double hours;
    double minutes;
    minutes = modf(posY, &hours) * 30;

    NSDateComponents *startComponents = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:weekday];
    [startComponents setHour:(int)hours];
    [startComponents setMinute:minutes];
    [setSecond:0];

    NSDate *currentDate = [CURRENT_CALENDAR dateFromComponents:startComponents];
    NSLog(@"%@", currentDate);
}

But, you need to modify MAWeekView.h by adding the property from .m :

@property (readonly) MAGridView *gridView;
@property (readonly) MAWeekdayBarView *weekdayBarView;

and

@protocol MAWeekViewDelegate <NSObject>

@optional
- (void)weekView:(MAWeekView *)weekView eventTapped:(MAEvent *)event;
- (void)weekView:(MAWeekView *)weekView weekDidChange:(NSDate *)week;
- (void)weekView:(MAWeekView *)weekView eventDragged:(MAEvent *)event;
- (BOOL)weekView:(MAWeekView *)weekView eventDraggingEnabled:(MAEvent *)event;

@end

@interface MAWeekdayBarView : UIView {
    MAWeekView *_weekView;
    NSDate *_week;
    UIColor *_textColor, *_sundayColor, *_todayColor;
    UIFont *_textFont;
    NSDateFormatter *_dateFormatter;
    NSMutableArray *_weekdays;
}