iOS 跑马灯效果

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view from its nib.


    //设置跑马灯的背景颜色

    self.view.backgroundColor = [UIColor whiteColor];


    [self addTwoLabelPaoMa];

}

//添加两个标签跑马灯

-(void) addTwoLabelPaoMa

{

    self.annonLabel= [[UILabelalloc]initWithFrame:CGRectMake(45,68,self.annonLabel.frame.size.width-45,30)];


    self.annonLabel.text = @"哎呦  我滴天啊,破鞋漏脚尖啊! 邻家小妹瞧见了是眼泪含眼圈啊......";

    self.annonLabel.font = [UIFont systemFontOfSize:16.0f];

    self.annonLabel.numberOfLines = 1;

    [self.annonLabel sizeToFit];



    self.secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.annonLabel.frame.origin.x+self.annonLabel.frame.size.width, self.annonLabel.frame.origin.y,self.annonLabel.frame.size.width,self.annonLabel.frame.size.height)];


    self.secondLabel.text = self.annonLabel.text;

    self.secondLabel.font = self.annonLabel.font;

    self.secondLabel.numberOfLines = 1;

    [self.secondLabel sizeToFit];


    [self.viewaddSubview:self.annonLabel];

    [self.view addSubview:self.secondLabel];


    [self.view sendSubviewToBack:self.annonLabel];

    [self.view sendSubviewToBack:self.secondLabel];


    [self addAnimation];

}

//添加跑马灯动画

-(void)addAnimation

{

    CGRectscrollFrame =self.annonLabel.frame;

    CGRectsecondFrame =self.secondLabel.frame;


    //10为动画时间

    [UIView animateWithDuration:10 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{

        self.annonLabel.frame = CGRectMake(-self.annonLabel.frame.size.width, self.annonLabel.frame.origin.y, self.annonLabel.frame.size.width, self.annonLabel.frame.size.height);


        //40是文字比较多,所以在消失出现设置距为40,文字较少可直接设置为0

        self.secondLabel.frame = CGRectMake(40, self.secondLabel.frame.origin.y, self.secondLabel.frame.size.width, self.secondLabel.frame.size.height);


    }completion:^(BOOLfinished) {

        self.annonLabel.frame= scrollFrame;

        self.secondLabel.frame= secondFrame;

        [self addAnimation];

    }];

}

你可能感兴趣的:(iOS 跑马灯效果)