1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Custom Date slider control

Discussion in 'iPhone/iPad Programming' started by arnieterm, Oct 26, 2009.

  1. macrumors regular

    For an iphone app, I have to create a custom calendar that shows a horizontal scroller with 5 dates [with day of month and weekday] displayed on them.
    I am using a scrollview with horizontal scrolling. My problem is that I need a data source that provides me a collection of dates for say 1 month so that when user swipes the control left to right or right to left and reaches either end than I remove the previous collection and add next set of the same.
    Can anyvody explain how to obtain the date data source. I only need dates equal to or less than current date with no future date.

  2. Moderator


    Staff Member

    Have you looked into NSDate?
  3. macrumors regular

    What function of NSDate class are you referring here? By date data source I mean to say date with some month range [not too big] that includes today's date and move backwards
  4. macrumors 68030


    You need to use a combination of NSDate, NSDateFormatter, NSCalendar and NSDateComponents.

    The NSDate class represents a specific date.
    The NSDateFormatter class allows you to format a date into a nice, human readable format.
    The NSCalendar class holds information about the calendar (e.g. gregorian) so that you can perform date calculations (such as what is the date in 30 days' time, 1 year, etc)
    The NSDateComponents class holds dates in terms of its component objects e.g. day, month, year, hour, minute, second.

    You can get today's date (and time) by calling
    NSDate *today = [NSDate date]
    You can then get the date components from it according to the correct calendar (look at NSCalendar) to split it up into day, month, year, etc.

    Then to get relative dates you can simply add on a day, or whatever

    To change a dateComponents representation of a date from today tomorrow, add 1 to its day component:
    [dateComponents setDay:[dateComponents day]+1]
    Now just convert back into a date using the appropriate NSCalendar method.

    Dates are a bit of a pain I'm afraid, and I'm sorry I'm being a bit lazy here but if you look at Apple's Date and Time Programming guide, you should be able to work out how to split a date into its components, do calculations on it, then create a new date. You then use NSDateFormatter to format it into a string that suits your preference.
  5. macrumors regular

    Giving a try to CFCalendarRef class for my problem. For a given CFAbsoluteTime value I need to calculate the the week day. I have tried using "CFCalendarGetOrdinalityOfUnit" method but so far no success.
    The above method enables us to get a smaller unit out of a bigger unit so how can I get the weekday value.
    CFCalendarGetOrdinalityOfUnit (currentCalendar,kCFCalendarUnitYear,kCFCalendarUnitEra, runningDate);	
    The above function call gives me year value. How to reterive weekday value also? Any idea
  6. macrumors member

    Check out the Time Utilities Reference. There are many useful functions, such as CFAbsoluteTimeGetDayOfWeek, which, if I understand correctly, I think does what you want.
  7. macrumors regular

    I am able to set up a UIScrollView to hold calendar data in horizontal fashion such that it shows two month dates [e.g. current month and previous month with dates upto today in case of current month]. I have adopted the logic here that after swiping enough and when user reaches start of previous month [left swiping]. I show up the month previous to previous month. The only oproblem is that contentSize is resizing the view in right hand side only and I have added the views corresponding to previous month on the extreme left hand side. Is there a way to extend the UIScrollView's content size so that it can extend to left hand side when needed instead of right hand side and extend as usual when adding month on the right hand side.

  8. Moderator


    Staff Member

    I feel there are two ways to achieve this: 1) increase the contentSize but then also shift all existing subviews over to the right by one page to make room for the new page, 2) already have contentSize set to some maximum size and then just add subviews as needed to allow display of new pages.
  9. macrumors regular

    I am able to create a scrolling date slider with two months dates now. I am clearing the scroll view's subviews before showing next/previous slot of dates and it is working fine. THe only thing that is disturbing is the slight halt in scrolling when one reaches at either extremes because in that case the app is in the process of removing the previous dates [UIView with weekday name and day of month label] with new slot. Is it possible to make scrolling smooth while also able to remove the older dates.
    Does using the below given methods with respect to the scrollview [though a completely different approach] will remove the halt problem?
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
  10. macrumors 68030


    Have you looked at the PageControl sample code? It shows how to swipe views left and right in an efficient way.

Share This Page