动态背景颜色UIImageView不更新iOS

动态背景颜色UIImageView不更新iOS,ios,swift3,uiimageview,uibackgroundcolor,Ios,Swift3,Uiimageview,Uibackgroundcolor,当我移动第一个UIImageView的视图光标时,我使用UIPangestureRecognitor获得点rgb的输出颜色。使用这种颜色,我必须在第二个uiview中创建一个渐变,从起始颜色的左侧开始,到白色的右侧。我注意到,在计算第一个rgb后,第二个UIView的背景色的颜色被校正,但在移动第一个UIView的光标并在rgb中获得更改后,颜色保持不变,但第二个UIView的背景色保持不变 扩展 我从这个例子开始创建渐变 您必须将所有子层设置为零 self.gradientview.ba

当我移动第一个UIImageView的视图光标时,我使用UIPangestureRecognitor获得点rgb的输出颜色。使用这种颜色,我必须在第二个uiview中创建一个渐变,从起始颜色的左侧开始,到白色的右侧。我注意到,在计算第一个rgb后,第二个UIView的背景色的颜色被校正,但在移动第一个UIView的光标并在rgb中获得更改后,颜色保持不变,但第二个UIView的背景色保持不变

扩展

我从这个例子开始创建渐变


您必须将所有子层设置为零

 self.gradientview.backgroundColor = UIColor.clear
 self.gradientview.layer.sublayers = nil

 UIView.animate(withDuration: 1, delay: 0.0, options:[.repeat, .autoreverse], animations: {

            self.gradientview.gradientBackground(from: color, to: UIColor.white, direction: GradientDirection.leftToRight)


   }, completion:nil)
  enum GradientDirection {
     case leftToRight
     case rightToLeft
    case topToBottom
    case bottomToTop
 }

 extension UIView {
     func gradientBackground(from color1: UIColor, to color2: UIColor, direction: GradientDirection) {
    let gradient = CAGradientLayer()
    gradient.frame = self.bounds
    gradient.colors = [color1.cgColor, color2.cgColor]

    switch direction {
    case .leftToRight:
        gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
        gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
    case .rightToLeft:
        gradient.startPoint = CGPoint(x: 1.0, y: 0.5)
        gradient.endPoint = CGPoint(x: 0.0, y: 0.5)
    case .bottomToTop:
        gradient.startPoint = CGPoint(x: 0.5, y: 1.0)
        gradient.endPoint = CGPoint(x: 0.5, y: 0.0)
    default:
        break
    }

    self.layer.insertSublayer(gradient, at: 0)
 }
}
 self.gradientview.backgroundColor = UIColor.clear
 self.gradientview.layer.sublayers = nil

 UIView.animate(withDuration: 1, delay: 0.0, options:[.repeat, .autoreverse], animations: {

            self.gradientview.gradientBackground(from: color, to: UIColor.white, direction: GradientDirection.leftToRight)


   }, completion:nil)