iOS
CAGradientLayer
수색…
통사론
- CAGradientLayer () // 초기화 된 CALayer 객체를 반환합니다.
- CAGradientLayer (layer : layer) // 지정된 레이어의 사용자 정의 필드를 복사하거나 초기화하려면 무시합니다.
매개 변수
매개 변수 | 세부 |
---|---|
색깔 | 각 그래디언트 정지의 색상을 정의하는 CGColorRef 객체의 배열입니다. Animatable. |
위치들 | 각 그라디언트 정지 위치를 정의하는 선택적 NSNumber 객체 배열입니다. Animatable. |
엔드 포인트 | 레이어의 좌표 공간에서 그릴 때 그라데이션의 끝점입니다. Animatable. |
시작점 | 레이어의 좌표 공간에서 그릴 때 그라데이션의 시작점입니다. Animatable. |
유형 | 레이어에 의해 그려지는 그라디언트의 스타일. 기본값은 kCAGradientLayerAxial 입니다. |
비고
-
startPoint
및endPoint
를 사용하여CAGradientLayer
의 방향을 변경합니다. -
locations
를 사용하여 색상의 확산 / 위치에 영향을줍니다.
CAGradientLayer 만들기
// View to hold the CAGradientLayer.
let view: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 320))
// Initialize gradient layer.
let gradientLayer: CAGradientLayer = CAGradientLayer()
// Set frame of gradient layer.
gradientLayer.frame = view.bounds
// Color at the top of the gradient.
let topColor: CGColor = UIColor.red.cgColor
// Color at the bottom of the gradient.
let bottomColor: CGColor = UIColor.yellow.cgColor
// Set colors.
gradientLayer.colors = [topColor, bottomColor]
// Set locations of the colors.
gradientLayer.locations = [0.0, 1.0]
// Insert gradient layer into view's layer heirarchy.
view.layer.insertSublayer(gradientLayer, at: 0)
결과 :
여러 색상으로 CGGradientLayer 만들기.
// View to hold the CAGradientLayer.
let view: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 320))
// Initialize gradient layer.
let gradientLayer: CAGradientLayer = CAGradientLayer()
// Set frame of gradient layer.
gradientLayer.frame = view.bounds
// Color at the top of the gradient.
let topColor: CGColor = UIColor.blue.cgColor
// Color at the middle of the gradient.
let middleColor: CGColor = UIColor.yellow.cgColor
// Color at the bottom of the gradient.
let bottomColor: CGColor = UIColor.green.cgColor
// Set colors.
gradientLayer.colors = [topColor, middleColor, bottomColor]
// Set locations of the colors.
gradientLayer.locations = [0.0, 0.5, 1.0]
// Insert gradient layer into view's layer heirarchy.
view.layer.insertSublayer(gradientLayer, at: 0)
결과 :
수평 CAGradientLayer 만들기.
// View to hold the CAGradientLayer.
let view: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 320))
// Initialize gradient layer.
let gradientLayer: CAGradientLayer = CAGradientLayer()
// Set frame of gradient layer.
gradientLayer.frame = view.bounds
// Color at the top of the gradient.
let topColor: CGColor = UIColor.redColor().CGColor
// Color at the bottom of the gradient.
let bottomColor: CGColor = UIColor.yellowColor().CGColor
// Set colors.
gradientLayer.colors = [topColor, bottomColor]
// Set start point.
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
// Set end point.
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
// Insert gradient layer into view's layer heirarchy.
view.layer.insertSublayer(gradientLayer, atIndex: 0)
결과 :
여러 색상으로 수평 CAGradientLayer 만들기.
// View to hold the CAGradientLayer.
let view: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 320))
// Initialize gradient layer.
let gradientLayer: CAGradientLayer = CAGradientLayer()
// Set frame of gradient layer.
gradientLayer.frame = view.bounds
// Color at the top of the gradient.
let topColor: CGColor = UIColor.greenColor().CGColor
// Color at the middle of the gradient.
let middleColor: CGColor = UIColor.blueColor().CGColor
// Color at the bottom of the gradient.
let bottomColor: CGColor = UIColor.blackColor().CGColor
// Set colors.
gradientLayer.colors = [topColor, middleColor, bottomColor]
// Set start point.
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
// Set end point.
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
// Insert gradient layer into view's layer heirarchy.
view.layer.insertSublayer(gradientLayer, atIndex: 0)
결과 :
CAGradientLayer에서 색상 변경 애니메이션.
// Get the current colors of the gradient.
let oldColors = self.gradientLayer.colors
// Define the new colors for the gradient.
let newColors = [UIColor.red.cgColor, UIColor.yellow.cgColor]
// Set the new colors of the gradient.
self.gradientLayer.colors = newColors
// Initialize new animation for changing the colors of the gradient.
let animation: CABasicAnimation = CABasicAnimation(keyPath: "colors")
// Set current color value.
animation.fromValue = oldColors
// Set new color value.
animation.toValue = newColors
// Set duration of animation.
animation.duration = 0.3
// Set animation to remove once its completed.
animation.isRemovedOnCompletion = true
// Set receiver to remain visible in its final state when the animation is completed.
animation.fillMode = kCAFillModeForwards
// Set linear pacing, which causes an animation to occur evenly over its duration.
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
// Set delegate of animation.
animation.delegate = self
// Add the animation.
self.gradientLayer.addAnimation(animation, forKey: "animateGradientColorChange")
결과 :
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow