美文网首页
UILabel的跑马灯效果

UILabel的跑马灯效果

作者: wrlynxayy | 来源:发表于2016-07-05 14:23 被阅读529次

方法一:使用UIView的动画

-(void)startAnim:(CGFloat)durationTime{

     [UIView beginAnimations:@"Marquee" context:NULL];

     [UIView setAnimationDuration:durationTime];

     [UIView setAnimationDelegate:self];

     [UIView setAnimationDidStopSelector:@selector(changeFrame)];

     [UIView setAnimationCurve:UIViewAnimationCurveLinear];

      [UIView setAnimationBeginsFromCurrentState:NO];

       [UIView setAnimationRepeatAutoreverses:NO];

        [UIView setAnimationRepeatCount:1];

        CGRect frame = self.tipLabel.frame;

       frame.origin.x = -frame.size.width;

       self.tipLabel.frame = frame;

       [UIView commitAnimations];

}

-(void)changeFrame{

       CGRect frame = self.tipLabel.frame;

       frame.origin.x = frame.size.width;

       self.tipLabel.frame = frame;

       [self startAnim:10.0f];

}

方法二:使用定时器NSTimer

// 提示信息轮播的定时器

@property (nonatomic, strong) NSTimer *timer;

- (void)setTimer {

        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self          selector:@selector(changePos) userInfo:nil repeats:YES];

}

// 通过改变X的位置来实现跑马灯效果

- (void)changePos {

          if (CGRectGetMaxX(self.tipLabel.frame) < 0) {

                    self.tipLabel.x = SCREEN_WIDTH - 60 - 60;

           } else {

                self.tipLabel.x = self.tipLabel.x - 5;

                }

}

有更好的办法 大家提一下  谢谢了!

相关文章

网友评论

      本文标题:UILabel的跑马灯效果

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