[英]Create a transparent Calendar with Tapku Calendar
首先,我想感謝您在本網站上提供的所有有用答案。 我大約六個月前開始編程,而我學到的許多東西都是來自這里的問題/答案。
我正在iPhone項目中使用Tapku Library中的Calendar,並且希望日歷磁貼是透明的,以便可以看到TKCalendarMonthView視圖后面的視圖。
我使用本傑明·皮爾森的本教程中的代碼實現了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代碼中的什么地方看? 我對Core Graphics不太熟悉,所以也許我缺少一些基本的/顯而易見的東西。
注意:我還嘗試過更改TKCalendarMonthView的tileBox(它是一個UIScrollView,似乎包含日歷圖塊)的顏色,盡管它確實更改了顏色,但不會影響圖塊的背景顏色。
提前致謝! 如果有任何不清楚的地方,請告訴我。
您可以按照以下步驟透明放置日歷圖塊
第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];
行之前[self.tileBox addSubview:self.currentTile];
在方法中- (void) _setupCurrentTileView:(NSDate*)date
所以你的代碼看起來像
- (void) _setupCurrentTileView:(NSDate*)date{
....
[self.currentTile setBackgroundColor:[UIColor clearColor]];
[self.tileBox addSubview:self.currentTile];
....
}
最后,我將了解如何在日歷視圖中使背景顏色清晰。
更改代碼如下。
在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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.