2

首先,我要感谢本网站上所有有用的答案。我大约六个月前开始编程,我学到的很多东西都是从这里的问题/答案中学到的。

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

我使用Benjamin Pearson编写的本教程中的代码实现了 TKCalendarMonthView 。

然后我从@Jacques 的这个答案中删除了平铺图像并尝试了代码,因此 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 代码中的哪个位置查找?我对核心图形不是很熟悉,所以也许我缺少一些基本/明显的东西。

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

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

4

2 回答 2

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]];

行前[self.tileBox addSubview:self.currentTile];

在方法中- (void) _setupCurrentTileView:(NSDate*)date

所以你的代码看起来像

- (void) _setupCurrentTileView:(NSDate*)date{

    ....

    [self.currentTile setBackgroundColor:[UIColor clearColor]];
    [self.tileBox addSubview:self.currentTile];

    ....

}
于 2013-11-16T10:24:50.887 回答
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];

}
于 2013-11-16T10:18:55.700 回答