[英]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.