美文网首页
Lottie 动画 基础篇——LOTAnimatedSwitch

Lottie 动画 基础篇——LOTAnimatedSwitch

作者: wangDavid939i | 来源:发表于2018-11-30 14:25 被阅读0次
😘切记!引入正确的头文件 哇呀呀~

#import <Lottie/Lottie.h>

  • 初始化按钮对象 并 设置 效果的 “起始” 和 “起始” 时间段
    (@"Switch")是本地资源中json文件的名称
LOTAnimatedSwitch *toggle1 = [LOTAnimatedSwitch switchNamed:@"Switch"];
  
  /// On animation is 0.5 to 1 progress. 动画是0.5到1的进度
  [toggle1 setProgressRangeForOnState:0.5 toProgress:1];
  /// Off animation is 0 to 0.5 progress.关闭动画是0到0.5进度
  [toggle1 setProgressRangeForOffState:0 toProgress:0.5];
  • 通过json文件 找到对应的图层的名字(nm)来为按钮的不同状态 进行自由的更改
 //    指定不同状态的图层名称
  [statefulSwitch setLayerName:@"Button" forState:UIControlStateNormal];
    
  [statefulSwitch setLayerName:@"Disabled" forState:UIControlStateDisabled];
  • 通过 更改 按钮的 “禁用”或“开始” 状态 效果 来更改 外观
  // Changes visual appearance by switching animation layer to "Disabled"
    
    //通过将动画层切换为“已禁用”来更改视觉外观
  statefulSwitch.enabled = NO;
  
  // Changes visual appearance by switching animation layer to "Button"
  statefulSwitch.enabled = YES;
  • 直接为 switch 的 按钮 来设置触发方法 并 添加到视图,并设置 按钮的位置
  [statefulSwitch addTarget:self action:@selector(switchToggled:) forControlEvents:UIControlEventValueChanged];
  [self.view addSubview:statefulSwitch];

statefulSwitch.center = 
        CGPointMake(CGRectGetMidX(self.view.bounds), 
        CGRectGetMaxY(heartIcon.frame) + 
        (statefulSwitch.bounds.size.height * 0.5));
  • 实现 按钮 点击后 响应的方法
//实现 按钮 点击后 响应的方法
- (void)switchToggled:(LOTAnimatedSwitch *)animatedSwitch {
  NSLog(@"The switch is %@", (animatedSwitch.on ? @"ON" : @"OFF"));
}

相关文章

网友评论

      本文标题:Lottie 动画 基础篇——LOTAnimatedSwitch

      本文链接:https://www.haomeiwen.com/subject/yhkhcqtx.html