iOS 小红点闪烁动画实现 - Objective-C 代码示例
iOS 小红点闪烁动画实现 - Objective-C 代码示例
本文将介绍使用 Objective-C 实现 iOS 小红点闪烁动画的代码示例。
代码实现
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UIView *redDotView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建小红点视图
self.redDotView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 10, 10)];
self.redDotView.backgroundColor = [UIColor redColor];
self.redDotView.layer.cornerRadius = 5;
self.redDotView.layer.masksToBounds = YES;
[self.view addSubview:self.redDotView];
// 开始小红点闪烁动画
[self startBlinkAnimation];
}
- (void)startBlinkAnimation {
// 创建闪烁动画
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: 'opacity'];
animation.fromValue = [NSNumber numberWithFloat:1.0];
animation.toValue = [NSNumber numberWithFloat:0.0];
animation.duration = 0.5;
animation.repeatCount = HUGE_VALF;
animation.autoreverses = YES;
[self.redDotView.layer addAnimation:animation forKey: 'blinkAnimation'];
}
@end
代码解析
在 viewDidLoad 方法中,首先创建了一个小红点视图 redDotView,并设置其样式和位置。然后调用 startBlinkAnimation 方法开始闪烁动画。
startBlinkAnimation 方法中,使用 CABasicAnimation 创建了一个透明度变化的动画,从不透明到透明,动画时间为 0.5 秒,重复次数为无限大,并设置了自动反转。最后将动画添加到小红点视图的 layer 上,并设置动画的 key 为 'blinkAnimation'。
总结
通过以上代码实现,我们成功地实现了 iOS 小红点一闪一闪的效果。您可以根据需要调整小红点的大小、颜色、位置和动画参数。
原文地址: https://www.cveoy.top/t/topic/0BY 著作权归作者所有。请勿转载和采集!