使用Tapku日历创建透明日历

时间:2013-09-08 23:20:56

标签: iphone ios objective-c calendar tapku

首先,我想对本网站上所有有用的答案表示感谢。我大约六个月前开始编程,我学到的很多东西都是来自问题/答案。

我在iPhone项目中使用Tapku Library中的日历,并希望日历图块是透明的,以便我可以看到我的TKCalendarMonthView视图背后的视图。

我使用Benjamin Pearson的this tutorial代码实现了TKCalendarMonthView。

然后我删除了平铺图像并尝试了@Jacques的this answer代码,因此TKCalendarMonthView.m中的drawrect函数如下所示:

- (void) drawRect:(CGRect)rect {

//From Jacques' StackOverflow answer (I also put this in the init)
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];


//From Jacques' answer
[[UIColor clearColor] setFill
 ];
UIRectFill(rect);

//Remove CGContextRef
    //CGContextRef context = UIGraphicsGetCurrentContext();
    //UIImage *tile = [UIImage imageWithContentsOfFile:TKBUNDLE(@"TapkuLibrary.bundle/Images/calendar/Month Calendar Date Tile.png")];

CGRect r = CGRectMake(0, 0, 46, 44);

//From Jacques' StackOverflow answer
[[UIColor clearColor] setFill];
UIRectFill(r);

//Remove this sense we won't use the tile image
//CGContextDrawTiledImage(context, r, tile.CGImage);


if(today > 0){
    int pre = firstOfPrev > 0 ? lastOfPrev - firstOfPrev + 1 : 0;
    int index = today +  pre-1;
    CGRect r =[self rectForCellAtIndex:index];
    r.origin.y -= 7;

    //Don't use image here
    //[[UIImage imageWithContentsOfFile:TKBUNDLE(@"TapkuLibrary.bundle/Images/calendar/Month Calendar Today Tile.png")] drawInRect:r];
}

int index = 0;

UIFont *font = [UIFont boldSystemFontOfSize:dateFontSize];
UIFont *font2 =[UIFont boldSystemFontOfSize:dotFontSize];

//Change the font for our dates:
font = [UIFont fontWithName:@"HelveticaNeue-Light" size:dateFontSize];
font2 = [UIFont fontWithName:@"HelveticaNeue-Light" size:dateFontSize];
UIColor *color = [UIColor grayColor];

if(firstOfPrev>0){
    [color set];
    for(int i = firstOfPrev;i<= lastOfPrev;i++){
        r = [self rectForCellAtIndex:index];
        if ([marks count] > 0)
            [self drawTileInRect:r day:i mark:[[marks objectAtIndex:index] boolValue] font:font font2:font2];
        else
            [self drawTileInRect:r day:i mark:NO font:font font2:font2];
        index++;
    }
}

//Set the color for all dates in the current month that are not today
color = [UIColor colorWithRed:59/255. green:73/255. blue:88/255. alpha:1];


[color set];
for(int i=1; i <= daysInMonth; i++){

    r = [self rectForCellAtIndex:index];
    if(today == i) [[UIColor whiteColor] set];

    if ([marks count] > 0) 
        [self drawTileInRect:r day:i mark:[[marks objectAtIndex:index] boolValue] font:font font2:font2];
    else
        [self drawTileInRect:r day:i mark:NO font:font font2:font2];
    if(today == i) [color set];
    index++;
}

[[UIColor grayColor] set];
int i = 1;
while(index % 7 != 0){
    r = [self rectForCellAtIndex:index] ;
    if ([marks count] > 0) 
        [self drawTileInRect:r day:i mark:[[marks objectAtIndex:index] boolValue] font:font font2:font2];
    else
        [self drawTileInRect:r day:i mark:NO font:font font2:font2];
    i++;
    index++;
}

}

问题是现在瓷砖(CGRects)是黑色的;或者直接在它们后面的任何视图都是黑色的,坦率地说,我在Tapku的代码中有点迷失。有谁知道为什么瓷砖是黑色的?或者我应该看看Tapku代码中的哪个位置?我对Core Graphics不是很熟悉,所以也许我错过了一些基本/明显的东西。

注意:我也尝试更改TKCalendarMonthView的tileBox(这是一个似乎包含日历图块的UIScrollView)的颜色,尽管它确实改变了颜色,但它并没有影响图块的背景颜色。

提前致谢!如果有任何不清楚的地方,请告诉我。

2 个答案:

答案 0 :(得分:3)

您可以按照以下步骤透明日历切片

<强>步骤1

注释TKCalendarMonthView.m中的代码

+ (void) initialize{
    if (self == [TKCalendarMonthTiles class]){
        //tileImage = [UIImage imageWithContentsOfFile:TKBUNDLE(@"calendar/Month Calendar Date Tile.png")];
    } 
}

<强>步骤-2

更改TKCalendarMonthView.m

中的代码

添加代码行[self.currentTile setBackgroundColor:[UIColor clearColor]];

行[{1}}

之前 方法[self.tileBox addSubview:self.currentTile];

中的

所以你的代码看起来像

- (void) _setupCurrentTileView:(NSDate*)date

答案 1 :(得分:1)

最后,我了解如何在日历视图中清除背景颜色。

更改代码如下。

TKCalendarMonthView.m

(1)评论/删除以下行。

+ (void) initialize
{
     if (self == [TKCalendarMonthTiles class])
     {
    //tileImage = [UIImage imageWithContentsOfFile:TKBUNDLE(@"calendar/Month Calendar Date Tile.png")];   COMMENT THIS LINE
     } 
}

(2) - (void) _setupCurrentTileView:(NSDate*)date 方法中添加 [self.currentTile setBackgroundColor:[UIColor clearColor]]; 行,如下所示

- (void) _setupCurrentTileView:(NSDate*)date{
if(self.currentTile) return;

NSDate *month = [date firstOfMonthWithTimeZone:self.timeZone];
NSArray *dates = [TKCalendarMonthTiles rangeOfDatesInMonthGrid:month startOnSunday:self.sunday timeZone:self.timeZone];
NSArray *data = [self.dataSource calendarMonthView:self marksFromDate:dates[0] toDate:[dates lastObject]];

self.currentTile = [[TKCalendarMonthTiles alloc] initWithMonth:month marks:data startDayOnSunday:self.sunday timeZone:self.timeZone];
[self.currentTile setTarget:self action:@selector(_tileSelectedWithData:)];

[self.currentTile setBackgroundColor:[UIColor clearColor]]; // ADD THIS LINE
[self.tileBox addSubview:self.currentTile];

self.monthYear.text = [date monthYearStringWithTimeZone:self.timeZone];
[self _updateSubviewFramesWithTile:self.currentTile];

}