2014-02-19 4 views
0
내가 여기 상단 대답에서 잡고 코드를 약간 수정 된 버전을 사용하고

작동하지 : Modern technique of adding gradient to UIView 내 수정 된 코드는그라데이션 알파의 drawRect

:

- (void)drawRect:(CGRect)rect 
{ 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // The if statements are surely a crappy way of setting the locations, but whatever 
    CGGradientRef gradient; 
    if ([self.colors count] == 3) { 
     CGFloat gradientLocations[] = {0, 0.5, 1}; 
     gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)self.colors, gradientLocations); 
    } else { 
     CGFloat gradientLocations[] = {0, 1}; 
     gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)self.colors, gradientLocations); 
    } 

    CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect)); 
    CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));  

    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0); 
} 

I 클래스에 있음을 뒀다 Gradient_View을 호출하고 서브 클래 싱을 시도하고 있습니다. 그래디언트가 투시 될 수 있도록해야하는 최종 배열의 제 2 색은, 0의 알파 값을 갖는 것으로

self.colors = [NSArray arrayWithObjects: 
      (id)[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:1.0f].CGColor, 
      [UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.0f].CGColor, 
       nil]; 

주 내 서브 init의 방법에서, 난이있다. 그러나, 나는 내 시뮬레이터에서 얻는 결과는 완전히 불투명 :

enter image description here

가 어떻게 투명도가 작동받을 수 있나요?

답변

4

UIView가 불투명한지 확인하고 확인하십시오. 불투명도가 NO로 설정되어 있는지 확인하십시오.

+0

간단했습니다 :) – maxedison

+0

나는 그 두 번 이상 치게되었습니다 :) –