你如何在 UIView 动画块中无限循环所有 属性 修改
How do you infinitely loop all property modifications within UIView animation blocks
我在为 UIView 元素制作发光动画时遇到问题无限增加我用来创建发光的对象阴影的大小和不透明度。
我试过使用不同的动画选项,但是 none 导致阴影属性无限变化,只有增加按钮大小的动画无限循环。
- (void)addGlow:(UIView *)element withColor:(UIColor *)color
{
element.layer.shadowColor = color.CGColor;
element.layer.shadowOpacity = 0;
element.layer.shadowOffset = CGSizeZero;
element.layer.shadowRadius = 0;
[UIView animateWithDuration:0.6 delay:0 options: UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState
animations:^
{
element.transform = CGAffineTransformMakeScale(1.02, 1.02);
element.layer.shadowOpacity = 0.5;
element.layer.shadowRadius = 5;
}
completion:NULL];
}
我基本上只希望 shadowOpacity 和 shadowRadius 也随着 UIView 对象的脉冲效果(由于转换)而无限增加和减少。
我会使用 CoreAnimation 的 CABasicAnimation + CAAnimationGroup,这是一个示例或我认为您正在尝试的内容:
@interface ViewController ()
@property (strong, nullable) IBOutlet UIButton *buttonToGlow;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.buttonToGlow.backgroundColor = [UIColor lightGrayColor];
self.buttonToGlow.layer.cornerRadius = 10.0f;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self addGlow:self.buttonToGlow withColor:[UIColor redColor]];
}
- (void)addGlow:(UIView *)element withColor:(UIColor *)color {
[element.layer removeAnimationForKey:@"glowAnimation"];
element.layer.shadowColor = color.CGColor;
element.layer.shadowOffset = CGSizeZero;
CABasicAnimation *shadowOpacityAnimation = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
shadowOpacityAnimation.fromValue = @(0);
shadowOpacityAnimation.toValue = @(0.5);
CABasicAnimation *shadowRadiusAnimation = [CABasicAnimation animationWithKeyPath:@"shadowRadius"];
shadowRadiusAnimation.fromValue = @(0);
shadowRadiusAnimation.toValue = @(5);
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = @(1.0);
scaleAnimation.toValue = @(1.02);
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.autoreverses = YES;
animationGroup.repeatCount = CGFLOAT_MAX;
animationGroup.duration = 0.6f;
animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animationGroup.animations = @[shadowOpacityAnimation, shadowRadiusAnimation, scaleAnimation];
[element.layer addAnimation:animationGroup forKey:@"glowAnimation"];
}
@end
结果如下:
参考文献:
CoreAnimation 编程指南:https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html?language=objc#//apple_ref/doc/uid/TP40004514
CABasicAnimation:https://developer.apple.com/documentation/quartzcore/cabasicanimation?language=objc
CAAnimationGroup:https://developer.apple.com/documentation/quartzcore/caanimationgroup?language=objc
我在为 UIView 元素制作发光动画时遇到问题无限增加我用来创建发光的对象阴影的大小和不透明度。
我试过使用不同的动画选项,但是 none 导致阴影属性无限变化,只有增加按钮大小的动画无限循环。
- (void)addGlow:(UIView *)element withColor:(UIColor *)color
{
element.layer.shadowColor = color.CGColor;
element.layer.shadowOpacity = 0;
element.layer.shadowOffset = CGSizeZero;
element.layer.shadowRadius = 0;
[UIView animateWithDuration:0.6 delay:0 options: UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState
animations:^
{
element.transform = CGAffineTransformMakeScale(1.02, 1.02);
element.layer.shadowOpacity = 0.5;
element.layer.shadowRadius = 5;
}
completion:NULL];
}
我基本上只希望 shadowOpacity 和 shadowRadius 也随着 UIView 对象的脉冲效果(由于转换)而无限增加和减少。
我会使用 CoreAnimation 的 CABasicAnimation + CAAnimationGroup,这是一个示例或我认为您正在尝试的内容:
@interface ViewController ()
@property (strong, nullable) IBOutlet UIButton *buttonToGlow;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.buttonToGlow.backgroundColor = [UIColor lightGrayColor];
self.buttonToGlow.layer.cornerRadius = 10.0f;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self addGlow:self.buttonToGlow withColor:[UIColor redColor]];
}
- (void)addGlow:(UIView *)element withColor:(UIColor *)color {
[element.layer removeAnimationForKey:@"glowAnimation"];
element.layer.shadowColor = color.CGColor;
element.layer.shadowOffset = CGSizeZero;
CABasicAnimation *shadowOpacityAnimation = [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];
shadowOpacityAnimation.fromValue = @(0);
shadowOpacityAnimation.toValue = @(0.5);
CABasicAnimation *shadowRadiusAnimation = [CABasicAnimation animationWithKeyPath:@"shadowRadius"];
shadowRadiusAnimation.fromValue = @(0);
shadowRadiusAnimation.toValue = @(5);
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = @(1.0);
scaleAnimation.toValue = @(1.02);
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
animationGroup.autoreverses = YES;
animationGroup.repeatCount = CGFLOAT_MAX;
animationGroup.duration = 0.6f;
animationGroup.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animationGroup.animations = @[shadowOpacityAnimation, shadowRadiusAnimation, scaleAnimation];
[element.layer addAnimation:animationGroup forKey:@"glowAnimation"];
}
@end
结果如下:
参考文献:
CoreAnimation 编程指南:https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html?language=objc#//apple_ref/doc/uid/TP40004514
CABasicAnimation:https://developer.apple.com/documentation/quartzcore/cabasicanimation?language=objc
CAAnimationGroup:https://developer.apple.com/documentation/quartzcore/caanimationgroup?language=objc