iOS Obj-c渐变背景过渡

时间:2020-08-08 13:46:44

标签: ios objective-c xcode animation gradient

我想逐渐将背景渐变从

gradient.colors = [NSArray arrayWithObjects:(id)[UIColor.blackColor CGColor],(id)[UIColor.blueColor CGColor], nil];

gradient.colors = [NSArray arrayWithObjects:(id)[UIColor.grayColor CGColor],(id)[UIColor.cyanColor CGColor], nil];

[编辑] ,我当前的代码是:

                gradient.colors = [NSArray arrayWithObjects:(id)[UIColor.blackColor CGColor],(id)[UIColor.blueColor CGColor], nil];
                NSArray *fromColors = gradient.colors;
                NSArray *toColors =[NSArray arrayWithObjects:(id)[UIColor.grayColor CGColor],(id)[UIColor.cyanColor CGColor], nil];
                CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: @"colors"];

                animation.fromValue = fromColors;
                animation.toValue = toColors;
                animation.duration = 3.00;
                animation.removedOnCompletion = false;
                animation.autoreverses = NO;
                animation.fillMode = kCAFillModeForwards;
                animation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear];
                if (![gradient.animationKeys containsObject:@"colors"]) {
                    [gradient addAnimation:animation forKey:@"colors"];
                }
                [self.background.layer addSublayer:gradient];

但是它什么也没做。有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

我不认为CABasicAnimation.keypath支持“颜色”。但是如何满足要求,我仍然想不出答案。https://stackoverflow.com/questions/5459673/how-can -i-cabasicanimation-keypath中的值