iOS状态栏翻滚文字

#import <UIKit/UIKit.h>

@interface CustomStatusBar : UIWindow
{
    UILabel *_statusMsgLabel;
}

- (void)showStatusMessage:(NSString *)message;
- (void)hide;

@end

 

#import "CustomStatusBar.h"

@implementation CustomStatusBar

- (void)dealloc
{
    [_statusMsgLabel release], _statusMsgLabel = nil;
    //
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.windowLevel = UIWindowLevelStatusBar + 1.0f;
  self.frame = [UIApplication sharedApplication].statusBarFrame;
        self.backgroundColor = [UIColor blackColor];
       
        _statusMsgLabel = [[UILabel alloc] initWithFrame:self.frame];
        _statusMsgLabel.backgroundColor = [UIColor clearColor];
        _statusMsgLabel.textColor = [UIColor whiteColor];
        _statusMsgLabel.font = [UIFont systemFontOfSize:10.0f];
        _statusMsgLabel.textAlignment = UITextAlignmentCenter;
        [self addSubview:_statusMsgLabel];
    }
    return self;
}

- (void)showStatusMessage:(NSString *)message
{
    self.hidden = NO;
    self.alpha = 1.0f;
    _statusMsgLabel.text = @"";
   
    CGSize totalSize = self.frame.size;
    self.frame = (CGRect){ self.frame.origin, 0, totalSize.height };
   
    [UIView animateWithDuration:0.5f animations:^{
        self.frame = (CGRect){ self.frame.origin, totalSize };
    } completion:^(BOOL finished){
        _statusMsgLabel.text = message;
    }];
}

- (void)hide
{
    self.alpha = 1.0f;
   
    [UIView animateWithDuration:0.5f animations:^{
        self.alpha = 0.0f;
    } completion:^(BOOL finished){
        _statusMsgLabel.text = @"";
        self.hidden = YES;
    }];;
}

@end

 

再配合定时器,以及UIScrollView来实现文字滚动,算法如下:

    CGPoint rt = pScroll.contentOffset;
    if (bBottom)
    { 
     rt.y += 4;
   }
    else
    {
        rt.y -= 4;
    }
   
    pScroll.contentOffset = rt;
    if (bBottom && rt.y >= 0)
    {
        [self Hide];
        [pTimer invalidate];
        pTimer = nil;
    }
   
    if (fabsf(rt.y) >= self.frame.size.height && !bBottom)
    {
        bBottom = YES;
    }

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值