2017-01-25 11 views
0

안녕하세요 저는 프로그래밍에 익숙하지 않아 UInt32 유형의 2 가지 색상 (예 : 빨간색과 파란색)이있는 그래디언트 효과를 만들 수 있는지 알고 싶습니다. 제안 사항이 있으십니까?UInt32 색상의 기울기 효과?

생각을 질문에 감사

ADDITION이 난 단지는 정보 픽셀을 가지고있다 : 지금

 func fillRegion(pixelX: Int, pixelY: Int, withColor color: UIColor) { 

    var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0 
    color.getRed(&red, green: &green, blue: &blue, alpha: &alpha) 

    let newColor = (UInt32)(alpha*255)<<24 | (UInt32)(red*255)<<16 | (UInt32)(green*255)<<8 | (UInt32)(blue*255)<<0 

    let pixelColor = regionsData.advanced(by: (pixelY * imageHeight) + pixelX).pointee 

    if pixelColor == blackColor { return } 

    var pointerRegionsData: UnsafeMutablePointer<UInt32> = regionsData 
    var pointerImageData: UnsafeMutablePointer<UInt32> = imageData 

    var pixelsChanged = false 

    for i in 0...(imageHeight * imageHeight - 1) { 
     if pointerRegionsData.pointee == pixelColor { 
      pointerImageData = imageData.advanced(by: i) 
      if pointerImageData.pointee != newColor { 
       pointerImageData.pointee = newColor 
       pixelsChanged = true 
      } 
     } 
     pointerRegionsData = pointerRegionsData.successor() 
    } 

    if pixelsChanged { 
     self.image = UIImage(cgImage: imageContext.makeImage()!) 
     DispatchQueue.main.async { 
      CATransaction.setDisableActions(true) 
      self.layer.contents = self.image.cgImage 
      self.onImageDraw?(self.image) 
     } 
     self.playTapSound() 
    } 
} 

간단 색으로 채우고,하지만 어떻게 든 그라데이션을 설정해야합니다, 난 몰라 적절한 양식을 가지고 있고 나는 여기에 색상을 바꿀 수 있지만 픽셀 수 정보와 그의 색만 가지고 있기 때문에 CAShapeLayer를 설정할 수는 없습니다 :

if pointerImageData.pointee != newColor { 
       pointerImageData.pointee = newColor 
       pixelsChanged = true 
      } 

그러나 부드럽게 색을 바꾸거나, 그라디언트 효과를 내거나, 어떻게 든 경계를 확인하고 레이어를 설정하는 법을 모르겠습니다. 몰라도 어떤 아이디어도 금처럼됩니다.

+1

"UInt32 Colors"란 무엇을 의미합니까? – Larme

+0

@Larme 유형의 색상 : var newColor = (UInt32) (alpha * 255) << 24 | (UInt32) (빨강 * 255) << 16 | (UInt32) (녹색 * 255) << 8 | (UInt32) (청색 * 255) << 0 –

+0

그래디언트를하려면, 정확하게 기억한다면 CGColor의 배열이 필요합니다. 따라서 colorClass를 UIColor로 변환 한 다음 CGColor로 변환해야합니다. – Larme

답변

0

CoreGraphics 또는 CALayer을 사용하면 가능합니다.
단순화하기 위해 CAGradientLayer를 반환하는보기의 레이어를 재정의 할 수 있습니다. 하나는 locations이고, 다른 하나는 당신이 gradindient에 추가 할 colors을, 당신은 속성으로 2 개 배열을 가지고

@implementation PGSGradientView 

+ (Class)layerClass { 
    return [CAGradientLayer class]; 
} 

- (instancetype) initWithCoder:(NSCoder *)aDecoder { 
    self = [super initWithCoder:aDecoder]; 
    if (self) { 
     [self initializeGradient]; 
    } 
    return self; 
} 

- (instancetype) initWithFrame:(CGRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     [self initializeGradient]; 
    } 
    return self; 
} 

- (void) initializeGradient { 
    [(CAGradientLayer*)self.layer setLocations:locations]; 
    [(CAGradientLayer*)self.layer setColors:colors]; 

} 

@end 

상상 :
이 구현입니다.
locations을 나타냄

각 색의 시작 위치. 또한 중요한 것은 이며 그 수는 0.0에서 1.0까지입니다. 이며 색상 배열과 쌍을 이룹니다.