【问题标题】:Is it possible to change UIImageView color while rotating image using CABasicAnimation?使用 CABasicAnimation 旋转图像时是否可以更改 UIImageView 颜色?
【发布时间】:2015-07-07 18:11:50
【问题描述】:

我可以使用 CABasicAnimation 旋转 UIImageView,但我想知道是否可以在图像旋转时更改图像的颜色,以便它转换为特定颜色?

【问题讨论】:

    标签: objective-c cabasicanimation


    【解决方案1】:

    是的,这是可能的。您可以将多个 CA 动画添加到一个对象。您还可以将它们放在一个组中,并作为一个组来控制时序和其他参数。

    例如:

    // make animation group
    let myAnimations = CAAnimationGroup()
    myAnimations.duration = 1
    myAnimations.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    
    // make opacity animation
    let opacity = CABasicAnimation(keyPath: "opacity")
    opacity.fromValue = 0
    opacity.toValue = 100
    
    // make a bg color animation
    let color = CABasicAnimation(keyPath: "backgroundColor")
    color.fromValue = UIColor.redColor().CGColor
    color.toValue = UIColor.blueColor().CGColor
    
    // make position animation
    let position = CABasicAnimation(keyPath: "position.y")
    position.fromValue = 0
    position.toValue = contentView.center.y - 80
    
    // add the animations to the group
    myAnimations.animations = [opacity, position, color]
    
    // add the group to the layer
    myObject.layer.addAnimation(placeAnimation, forKey: "myAnimations")
    

    【讨论】:

    • 谢谢克里斯,我不知道你可以将多个动画添加为一个数组。我想我应该澄清颜色部分,我该怎么做才能改变 UIImageView 的颜色?我尝试过应用色调并设置背景颜色。
    • 您是否要在 UIImageView 中改变 UIImage 的颜色?
    • 是的,所以它覆盖了图像的所有不透明部分,而不是透明部分,即动态蒙版。我的意思是我猜非完全编码的方式是将图像与我已经想要的颜色的蒙版重叠,但将不透明度从 0 转换为 1...
    • // 图像是您设置为 UIImageView 图像的 UIImage img = img.imageWithRenderingMode(.AlwaysTemplate) let imageView = UIImageView(image: image) imageView.tintColor = UIColor.redColor( ) imageView.image = image 抱歉,显然我无法按照我想要的方式格式化 cmets 中的代码。 prntscr.com/7pycyt 就是这个样子
    • 这就是我想要的,但奇怪的是,设置渲染模式 myImg = [myImg imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] 不会显示任何内容。UIImageView 的模式是缩放填充 + 绘图不透明。我已经按名称设置了图像,并使用已经设置的现有图像进行了测试
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    • 2011-09-27
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    相关资源
    最近更新 更多