animation跑马灯动画实现两种方法

//方法一
@interface ViewController ()

@end

@implementation ViewController
{
    UILabel * _label ;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _label  = [[UILabel alloc]initWithFrame:CGRectMake(0, 30, 100, 40)];
    _label.text = @"游来游去";
    _label.textAlignment= NSTextAlignmentCenter;
    _label.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:_label];
    
    //实现从左到右
    [self leftToRight];
}

-(void)rightToLeft
{
    //从右到左
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:4];
    _label.frame = CGRectMake(0, 30, 100, 40);
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(leftToRight)];
    [UIView commitAnimations];
    
}

-(void)leftToRight
{
    //实现从左到右
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:4];
    _label.frame = CGRectMake(320, 30, 100, 40);
    //设置谁去调用动画的结束方法(设置代理)
    [UIView setAnimationDelegate:self];
    //设置动画将要开始的方法
    [UIView setAnimationWillStartSelector:@selector(animationStart)];
    //设置动画的结束方法  谁什么情况下做什么事情
    [UIView setAnimationDidStopSelector:@selector(rightToLeft)];
    [UIView commitAnimations];
    
    NSLog(@"动画块的代码执行完了,但是动画还没有结束 ;代码的执行 和 动画的执行 是  并列 的");
}

-(void)animationStart
{
    NSLog(@"动画要开始了");
}


@end


 

//方法二
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 100, 40)];
    label.backgroundColor = [UIColor purpleColor] ;
    label.text = @"跑马灯";
    [self.view addSubview:label];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:2];
    
    //设置动画的重复次数  注意顺序;写在结束坐标点的上方;
    //LONG_MAX:无穷大
    [UIView setAnimationRepeatCount:LONG_MAX];
    //设置是否执行反向动画 ;
    [UIView setAnimationRepeatAutoreverses:YES];
    
    label.frame = CGRectMake(320, 20, 100, 40);
    [UIView commitAnimations];


  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值