[英]Create a transparent Calendar with Tapku Calendar
First I wanna say thanks for all the helpful answers on this site. 首先,我想感谢您在本网站上提供的所有有用答案。 I starting programming about six months ago and many of the things I've learned have been from questions/answers here.
我大约六个月前开始编程,而我学到的许多东西都是来自这里的问题/答案。
I'm using the Calendar from Tapku Library in an iPhone project and want the calendar tiles to be transparent so that I can see the view behind my TKCalendarMonthView view. 我正在iPhone项目中使用Tapku Library中的Calendar,并且希望日历磁贴是透明的,以便可以看到TKCalendarMonthView视图后面的视图。
I implemented the TKCalendarMonthView using the code from this tutorial by Benjamin Pearson. 我使用本杰明·皮尔森的本教程中的代码实现了TKCalendarMonthView。
Then I removed the tile images and tried code from this answer by @Jacques, so the drawrect function in TKCalendarMonthView.m looks like this: 然后我通过@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++;
}
}
The problem is that now the tiles (CGRects) are black; 问题在于,现在磁贴(CGRects)是黑色的。 or whatever view is directly behind the them is black, and, frankly, I'm a bit lost in Tapku's code.
或它们后面的任何视图都是黑色的,坦率地说,我对Tapku的代码有些迷惑。 Does anyone know why the tiles are black?
有谁知道为什么瓷砖是黑色的? Or where in the Tapku code I should be looking?
还是我应该在Tapku代码中的什么地方看? I'm not very familiar with Core Graphics, so maybe I'm missing something basic/obvious.
我对Core Graphics不太熟悉,所以也许我缺少一些基本的/显而易见的东西。
NOTE: I also tried changing the color of TKCalendarMonthView's tileBox (which is a UIScrollView that seems to contain the calendar tiles) and although it did change color it didn't effect the tiles' background color. 注意:我还尝试过更改TKCalendarMonthView的tileBox(它是一个UIScrollView,似乎包含日历图块)的颜色,尽管它确实更改了颜色,但不会影响图块的背景颜色。
Thanks in advance! 提前致谢! And please let me know if any of this is unclear.
如果有任何不清楚的地方,请告诉我。
You can transparent calendar tiles by following steps 您可以按照以下步骤透明放置日历图块
Step-1 第1步
Comment the code in TKCalendarMonthView.m 注释TKCalendarMonthView.m中的代码
+ (void) initialize{
if (self == [TKCalendarMonthTiles class]){
//tileImage = [UIImage imageWithContentsOfFile:TKBUNDLE(@"calendar/Month Calendar Date Tile.png")];
}
}
Step-2 第2步
Change the code in TKCalendarMonthView.m 更改TKCalendarMonthView.m中的代码
add line of code [self.currentTile setBackgroundColor:[UIColor clearColor]];
添加一行代码
[self.currentTile setBackgroundColor:[UIColor clearColor]];
before the line [self.tileBox addSubview:self.currentTile];
在
[self.tileBox addSubview:self.currentTile];
行之前[self.tileBox addSubview:self.currentTile];
in method - (void) _setupCurrentTileView:(NSDate*)date
在方法中
- (void) _setupCurrentTileView:(NSDate*)date
so your code look like 所以你的代码看起来像
- (void) _setupCurrentTileView:(NSDate*)date{
....
[self.currentTile setBackgroundColor:[UIColor clearColor]];
[self.tileBox addSubview:self.currentTile];
....
}
Finally I get how to make the background color clear in calender view. 最后,我将了解如何在日历视图中使背景颜色清晰。
Change code as below. 更改代码如下。
In TKCalendarMonthView.m 在TKCalendarMonthView.m中
(1) Comment/remove below line. (1)在下面的行中注释/删除。
+ (void) initialize
{
if (self == [TKCalendarMonthTiles class])
{
//tileImage = [UIImage imageWithContentsOfFile:TKBUNDLE(@"calendar/Month Calendar Date Tile.png")]; COMMENT THIS LINE
}
}
(2) In - (void) _setupCurrentTileView:(NSDate*)date
method (2)在
- (void) _setupCurrentTileView:(NSDate*)date
方法中
Add [self.currentTile setBackgroundColor:[UIColor clearColor]];
添加
[self.currentTile setBackgroundColor:[UIColor clearColor]];
line as below 行如下
- (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.