动态backgroundColor UIImageView不更新iOS

时间:2017-07-18 08:27:21

标签: ios swift3 uiimageview uibackgroundcolor

当我移动第一个UIImageView的视图光标时,我使用UIPanGestureRecognizer输出点rgb的颜色。使用这种颜色,我必须在第二个uiview中创建一个渐变,它从起始颜色的左边开始,到右边的白色。我注意到在计算出第一个rgb之后,第二个UIView的backgroundColor的颜色被修正了但是在移动了第一个UIView的光标并且在rgb中获得了颜色的变化,但是第二个uiview的backgroundColor保持不变。

enter image description here

  @objc func wasDragged(gestureRecognizer: UIPanGestureRecognizer) {

    if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {

        let translation = gestureRecognizer.translation(in: self.view)

        gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y)
        gestureRecognizer.setTranslation(CGPoint.zero, in: self.view)
        let color = self.imageView.getPixelColorAt(point: gestureRecognizer.view!.center)
       // color is correct

        UIView.animate(withDuration: 1, delay: 0.0, options:[.repeat, .autoreverse], animations: {
             self.gradientview.backgroundColor = UIColor.clear
             self.gradientview.gradientBackground(from: color, to:   UIColor.white, direction: GradientDirection.leftToRight)
       // color is correct only first time, after remains unchanged
        }, completion:nil)

    }
 }

在扩展程序中

我从这个例子开始创建渐变 Programmatically create a UIView with color gradient

  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)
 }
}

1 个答案:

答案 0 :(得分:0)

您必须设置为nil all sublayer

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Dim pt As PivotTable
    For Each pt In ActiveSheet.PivotTables
        columns.autofit
    Next pt
End Sub