Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 设置背景颜色更改动画-从一侧到另一侧_Ios_Swift_Uiviewanimation - Fatal编程技术网

Ios 设置背景颜色更改动画-从一侧到另一侧

Ios 设置背景颜色更改动画-从一侧到另一侧,ios,swift,uiviewanimation,Ios,Swift,Uiviewanimation,假设我有一个水平长的矩形。它是蓝色的。我想要红色的。但是,我希望颜色更改从一侧开始,在另一侧结束 我可以使用关键帧动画使整个视图逐渐从红色变为蓝色。有没有办法从左到右/从右到左逐渐改变它 UIView.animateKeyframesWithDuration(2.0 /*Total*/, delay: 0.0, options: UIViewKeyframeAnimationOptions.CalculationModeLinear, animations: { UIView.addKe

假设我有一个水平长的矩形。它是蓝色的。我想要红色的。但是,我希望颜色更改从一侧开始,在另一侧结束

我可以使用关键帧动画使整个视图逐渐从红色变为蓝色。有没有办法从左到右/从右到左逐渐改变它

UIView.animateKeyframesWithDuration(2.0 /*Total*/, delay: 0.0, options: UIViewKeyframeAnimationOptions.CalculationModeLinear, animations: {
    UIView.addKeyframeWithRelativeStartTime(0.0, relativeDuration: 1/1, animations:{
        self.view.backgroundColor = UIColor.redColor()    
        self.view.layoutIfNeeded()
    })
    },
     completion: { finished in
        if (!finished) { return }
})
看一看。您可以设置其
位置
颜色
端点
起始点

这里是一个快速的片段,你们可以粘贴到操场上,看看它是如何工作的。在本例中,我将设置渐变颜色位置的动画

import UIKit
import XCPlayground

XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 400))

let startLocations = [0, 0]
let endLocations = [1, 2]

let layer = CAGradientLayer()
layer.colors = [UIColor.redColor().CGColor, UIColor.blueColor().CGColor]
layer.frame = view.frame
layer.locations = startLocations
layer.startPoint = CGPoint(x: 0.0, y: 1.0)
layer.endPoint = CGPoint(x: 1.0, y: 1.0)
view.layer.addSublayer(layer)

let anim = CABasicAnimation(keyPath: "locations")
anim.fromValue = startLocations
anim.toValue = endLocations
anim.duration = 2.0
layer.addAnimation(anim, forKey: "loc")
layer.locations = endLocations

XCPlaygroundPage.currentPage.liveView = view
Swift 3版本:

import UIKit
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true


let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 400))

let startLocations = [0, 0]
let endLocations = [1, 2]

let layer = CAGradientLayer()
layer.colors = [UIColor.red.cgColor, UIColor.blue.cgColor]
layer.frame = view.frame
layer.locations = startLocations as [NSNumber]?
layer.startPoint = CGPoint(x: 0.0, y: 1.0)
layer.endPoint = CGPoint(x: 1.0, y: 1.0)
view.layer.addSublayer(layer)

let anim = CABasicAnimation(keyPath: "locations")
anim.fromValue = startLocations
anim.toValue = endLocations
anim.duration = 2.0
layer.add(anim, forKey: "loc")
layer.locations = endLocations as [NSNumber]?

PlaygroundPage.current.liveView = view