- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试使用 CABasicAnimation 为 CAGradientLayer 的位置数组设置动画。文档说这是可行的。我正在使用以下代码:
CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"locations"];
fadeAnim.fromValue = _gradientLayer.locations;
fadeAnim.toValue = @[[NSNumber numberWithFloat:0.8f], [NSNumber numberWithFloat:1.0f]];
fadeAnim.duration = 3.0;
fadeAnim.delegate = self;
[fadeAnim setFillMode:kCAFillModeForwards];
[fadeAnim setDuration:3.0f];
[fadeAnim setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[_gradientLayer addAnimation:fadeAnim forKey:@"animation"];
现在,我认为这可能是一个奇怪的层/子层情况,所以我添加了以下代码来为颜色设置动画
CABasicAnimation* fadeAnim1 = [CABasicAnimation animationWithKeyPath:@"colors"];
fadeAnim1.toValue = @[(id)[UIColor greenColor].CGColor,(id)[transparentColor CGColor],(id)[UIColor blueColor].CGColor];
fadeAnim1.duration = 5.0;
[_gradientLayer addAnimation:fadeAnim1 forKey:@"colorAnimation"];
颜色动画很好,当调用第一个位置动画的委托(delegate)时。
NSString *keyPath = ((CAPropertyAnimation *)anim).keyPath;
if ([keyPath isEqualToString:@"locations"]) {
_gradientLayer.locations = ((CABasicAnimation *)anim).toValue;
}
正确设置位置的最终状态,但没有动画
最佳答案
我下载了你的示例项目并看了一下。
它有两个问题:
locations
数组中的对象数量必须与 colours
数组中的对象数量相同。在您的示例中,您有三种颜色和两个位置,因此核心动画无法计算出要执行的正确动画。所以我更新了以下几行:
NSArray * startingLocations = @[[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:0.4f],[NSNumber numberWithFloat:1.0f]];
和
fadeAnim.toValue = @[[NSNumber numberWithFloat:0.0f],[NSNumber numberWithFloat:0.8f], [NSNumber numberWithFloat:1.0f]];
这是您的位置没有动画的主要原因
动画中还有一些不必要的复杂性。解释的很好here ,但在这种情况下您不需要填充模式或委托(delegate)。您将位置设置为最终值(这会更新模型层,但会创建隐式动画)。然后,您使用相同的键(在您的情况下为位置)创建一个显式动画,并将 from 值设置为您的起始数组。这会覆盖隐式动画。
我修改后的动画代码是这样的:
//Create gradient layer
_gradientLayer = [CAGradientLayer layer];
_gradientLayer.frame = maskedImageView.bounds;
_gradientLayer.colors = colors;
//set locations for the colors
NSArray * startingLocations = @[@0.0, @0.4,@1.0];
NSArray *endinglocations = @[@0.0,@0.8,@1.0];
// Update the model layer to the final point
_gradientLayer.locations = endinglocations;
_gradientLayer.startPoint = CGPointMake(0.0f, 0.3f);
_gradientLayer.endPoint = CGPointMake(1.0f, 0.5f);
//add the text image as a mask on the gradient layer
_gradientLayer.mask = maskedImageView.layer;
//add the gradient layer to the holder view
[_slideImageView.layer addSublayer:_gradientLayer];
//Create animation for the "locations" aspect of the gradient layer
CABasicAnimation* fadeAnim = [CABasicAnimation animationWithKeyPath:@"locations"];
//Set the starting locations to the previously applied array
fadeAnim.fromValue = startingLocations;
// 3 seconds of run time
[fadeAnim setDuration:3.0f];
//linear and straight timing since we want it smooth
[fadeAnim setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
//do the magic and apply the animation
[_gradientLayer addAnimation:fadeAnim forKey:@"locations"];
关于ios - CAGradientLayer 动画 "colors"但不是 "locations",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20337793/
ios渐变圆环旋转动画cashapelayer cagradientlayer shape.gif demo.png ?
我正在尝试在 View 上设置渐变背景,但下面的代码使 UIView 显示为纯黑色。如果我将白色更改为绿色,渐变将正确显示。将图层的背景颜色更改为 greenColor 会使 View 显示为纯绿色。
我正在尝试创建一个具有渐变背景的 UITableViewCell。我不明白为什么,但 UITableView 的右侧有一个空白区域。我尝试通过设置纯色但结果没有改变。 这是我的 UITableView
我的问题很简单。 我想使用 CAGradientLayer 为我的 UIView 制作漂亮的渐变背景。 但问题是它显示出丑陋的紫罗兰色,而不是我想要的美妙的红色。 这是我正在使用的代码: - (voi
我使用下面的代码来设置我的 View 的渐变背景颜色。 CAGradientLayer *gradient = [CAGradientLayer layer]; gradient.frame
我正在尝试创建两个渐变层,一个位于 ScrollView 的每一侧,以显示有要滚动到的内容。 左侧的效果很好,但右侧的显示完全透明。我错过了什么? CAGradientLayer *l = [CAGr
好的,我有一个 Collection View 和一个使用 Xib 文件创建的自定义单元格,以便我可以在不同 View 的另一个 Collection View 中使用相同的单元格。在我的 cellF
关于CAGradientLayerlocations属性的文档指定它必须设置为从0到1的NSNumber数组,但它实际上适用于负数和大于1的数字。这样使用安全吗? 它按照您期望的方式工作 gradie
我有以下代码来呈现一个CAGradientLayer,它应该覆盖整个 View 。但是,它在模拟器中显示不正确。我打印了 self.view.bounds.width 和 self.view.boun
您好,我正在尝试从该颜色设置为另一个 View 的渐变中获取颜色。我可以设置开始和结束颜色,但不能设置角度和类型。 以下是值:1.开始颜色:@“2b2b2b”2.结束颜色:@“4a4a4a”3.渐变角
我有一个像 Instagram 个人资料图片中那样的圈子,里面有故事。我想要有一种像圆圈在旋转的感觉。为此,我使用了 CABasicAnimation。它正在旋转,但不在中心。 正如我搜索的那样,我需
我正在使用代码 NSArray *buttons = [NSArray arrayWithObjects: self.rollBtn,nil]; for(UIButton *btn in
我正在为我的 View 背景设置渐变: CAGradientLayer *grad = [CAGradientLayer layer]; grad.frame = self.contentView.f
CAGradientLayer 有两个属性startPoint 和endPoint。这些属性是根据单位坐标空间定义的。结果是,如果我有两个具有相同起点和终点且每个边界不同的渐变层,则这两个渐变将不同。
我用CAShapeLayer画了一个圆,并设置为CAGradientLayer的mask,代码如下: CAShapeLayer *circleShapeLayer = [CAShapelayer ne
我使用 CAGradientLayer 来生成这个细线渐变。问题是自转不顺畅。当 UIViewController 旋转时,我在 viewDidLayoutSubviews 中设置包含该层的 View
我使用 CAGradientLayer 作为按钮的背景。 cell.showOnMap.clipsToBounds = YES; cell.showOnMap.layer.cornerRadius =
我想要一个彩色渐变来覆盖我的 View 。在 View Controller 中,我有这段代码 - (void)viewDidLoad { self.view.backgroundColor =
我想为我的 UIView 创建一个渐变作为背景色。我写了这段代码 gradient.colors = [UIColor.blue,UIColor.yellow] gradient.lo
有一个CAShapeLayer 当我将它添加为 mask 时,我得到一个空白屏幕 这是我的代码: let width: CGFloat = frame.width let radius
我是一名优秀的程序员,十分优秀!