//方法一
@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];