iphone - 在 iOS 7 下运行 iOS 6 应用程序时,为什么阴影无法正确显示在我的 View 上?

标签 iphone ios objective-c cocoa-touch ios7

我有一个 iOS 6 应用程序,尚未更新到 iOS 7。我的阴影有一些奇怪的颜色问题,这些问题只出现在 iOS 7 上。有时它们看起来正常,有时它们是彩色的。

正常(iOS 7 上阴影正常出现的时间为 50%):

enter image description here

有色(50% 的情况下它们会以这种方式出现。它们应该像上面那样是黑色的。似乎在移动 View 时会发生这种情况):

enter image description here enter image description here

大家有什么想法吗?这是我已经使用两年多的代码。有一个更好的方法吗?此处是否存在不正确的更新 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/

相关文章:

ios - 为什么 magnetic Field.field 的值固定为零?

用于检查铁路 PNR 状态和列车查询的 iPhone 应用程序

ios - 以编程方式添加 subview 后自动调整单元格大小

ios - FIrebase 3 查询以返回与子键/值匹配的值

ios - 使用NSPredicate过滤NSDictionaries的NSDictionary

objective-c - 在 WatchKit 应用程序中使用来自 Web API 的数据动态加载 View

objective-c - 如果窗口不是最前面的,则获取 beginGestureWithEvent/endGestureWithEvent 的方法

ios - 在 iOS 上发送 HTTP POST 请求并从服务器接收响应

iphone - 实例化子 Controller

iphone - 如果用户 "flags"来 self 的 iOS 应用程序的某些 Material ,我可以从 parse.com 获得某种通知吗?