我有一个 iOS 6 应用程序,尚未更新到 iOS 7。我的阴影有一些奇怪的颜色问题,这些问题只出现在 iOS 7 上。有时它们看起来正常,有时它们是彩色的。
正常(iOS 7 上阴影正常出现的时间为 50%):
有色(50% 的情况下它们会以这种方式出现。它们应该像上面那样是黑色的。似乎在移动 View 时会发生这种情况):
大家有什么想法吗?这是我已经使用两年多的代码。有一个更好的方法吗?此处是否存在不正确的更新 API 调用?
ShadowedTableView.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface ShadowedTableView : UITableView
{
CAGradientLayer *originShadow;
CAGradientLayer *topShadow;
CAGradientLayer *bottomShadow;
}
@end
ShadowedTableView.m
#import "ShadowedTableView.h"
#define SHADOW_HEIGHT 20.0
#define SHADOW_INVERSE_HEIGHT 10.0
#define SHADOW_RATIO (SHADOW_INVERSE_HEIGHT / SHADOW_HEIGHT)
@implementation ShadowedTableView
//
// shadowAsInverse:
//
// Create a shadow layer
//
// Parameters:
// inverse - if YES then shadow fades upwards, otherwise shadow fades downwards
//
// returns the constructed shadow layer
//
- (CAGradientLayer *)shadowAsInverse:(BOOL)inverse
{
CAGradientLayer *newShadow = [[CAGradientLayer alloc] init];
CGRect newShadowFrame =
CGRectMake(0, 0, self.frame.size.width,
inverse ? SHADOW_INVERSE_HEIGHT : SHADOW_HEIGHT);
newShadow.frame = newShadowFrame;
newShadow.colors =
@[(__bridge id)(inverse ?
([self.backgroundColor colorWithAlphaComponent:0.0].CGColor) :
([UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:inverse ? (SHADOW_INVERSE_HEIGHT / SHADOW_HEIGHT) * 0.5 : 0.5].CGColor)),
(__bridge id)(inverse ?
([UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:inverse ? (SHADOW_INVERSE_HEIGHT / SHADOW_HEIGHT) * 0.5 : 0.5].CGColor) :
([self.backgroundColor colorWithAlphaComponent:0.0].CGColor))];
return newShadow;
}
//
// layoutSubviews
//
// Override to layout the shadows when cells are laid out.
//
- (void)layoutSubviews
{
[super layoutSubviews];
//
// Construct the origin shadow if needed
//
if (!originShadow)
{
originShadow = [self shadowAsInverse:NO];
[self.layer insertSublayer:originShadow atIndex:0];
}
else if (![(self.layer.sublayers)[0] isEqual:originShadow])
{
[self.layer insertSublayer:originShadow atIndex:0];
}
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
//
// Stretch and place the origin shadow
//
CGRect originShadowFrame = originShadow.frame;
originShadowFrame.size.width = self.frame.size.width;
originShadowFrame.origin.y = self.contentOffset.y;
originShadow.frame = originShadowFrame;
[CATransaction commit];
if (self.style == UITableViewStylePlain)
{
NSArray *indexPathsForVisibleRows = [self indexPathsForVisibleRows];
if ([indexPathsForVisibleRows count] == 0)
{
[topShadow removeFromSuperlayer];
topShadow = nil;
[bottomShadow removeFromSuperlayer];
bottomShadow = nil;
return;
}
NSIndexPath *firstRow = indexPathsForVisibleRows[0];
if ([firstRow section] == 0 && [firstRow row] == 0)
{
UIView *cell = [self cellForRowAtIndexPath:firstRow];
if (!topShadow)
{
topShadow = [self shadowAsInverse:YES];
[cell.layer insertSublayer:topShadow atIndex:0];
}
else if ([cell.layer.sublayers indexOfObjectIdenticalTo:topShadow] != 0)
{
[cell.layer insertSublayer:topShadow atIndex:0];
}
CGRect shadowFrame = topShadow.frame;
shadowFrame.size.width = cell.frame.size.width;
shadowFrame.origin.y = -SHADOW_INVERSE_HEIGHT;
topShadow.frame = shadowFrame;
}
else
{
[topShadow removeFromSuperlayer];
topShadow = nil;
}
NSIndexPath *lastRow = [indexPathsForVisibleRows lastObject];
if ([lastRow section] == [self numberOfSections] - 1 &&
[lastRow row] == [self numberOfRowsInSection:[lastRow section]] - 1)
{
UIView *cell =
[self cellForRowAtIndexPath:lastRow];
if (!bottomShadow)
{
bottomShadow = [self shadowAsInverse:NO];
[cell.layer insertSublayer:bottomShadow atIndex:0];
}
else if ([cell.layer.sublayers indexOfObjectIdenticalTo:bottomShadow] != 0)
{
[cell.layer insertSublayer:bottomShadow atIndex:0];
}
CGRect shadowFrame = bottomShadow.frame;
shadowFrame.size.width = cell.frame.size.width;
shadowFrame.origin.y = cell.frame.size.height;
bottomShadow.frame = shadowFrame;
}
else
{
[bottomShadow removeFromSuperlayer];
bottomShadow = nil;
}
}
}
//
// dealloc
//
// Releases instance memory.
//
@end
最佳答案
似乎 CAGradientLayer 需要 {0,0,0,0} 的 RGBA 来制作完全透明的颜色,这意味着它的混合模式在 iOS 7 中发生了变化。我已经为此提交了雷达 (#15336983)。
无论如何,解决方法是替换:
([self.backgroundColor colorWithAlphaComponent:0.0].CGColor) :
与:
([UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0].CGColor) :
关于iphone - 在 iOS 7 下运行 iOS 6 应用程序时,为什么阴影无法正确显示在我的 View 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19521898/