iOS 跑馬燈捲軸效果程式碼

來源:互聯網
上載者:User

跑馬燈效果的捲軸,一般出現在ios應用的底部。用於顯示動態變化的資訊或內容較長的資訊,在個類應用中使用廣泛
以下兩種可用的跑馬燈滾動MarqueeBar的實現。

1.直接在ViewController中實現對UIView的位置定時移動來實現,以下代碼直接加入到ViewController中,在viewWillAppear中調用loadView即可。

 

 代碼如下 複製代碼
- (void)marqueeView
{
 
    CGRect frame = self.vMarqueeContainer.frame;
    frame.origin.x = frame.origin.x -2;
    if(frame.origin.x < -frame.size.width )
    {
        frame.origin.x  = 320;
    }
    self.vMarqueeContainer.frame = frame;
 
    //延時遞迴調用
    [self performSelector:@selector(marqueeView) withObject:nil afterDelay:0.04];
}
 
- (void)loadView
{
 //marqueenbar背景,位置高度等控制
    UIView *viewMarqueeBar = [[[UIView alloc]initWithFrame:CGRectMake(0, 347, 320, 20)]autorelease];
    [viewMarqueeBar setBackgroundColor:[UIColor darkGrayColor]];
 
    //滾動容器,顯示滾動範圍
    UIView *viewMarqueeContainer = [[[UIView alloc]initWithFrame:CGRectMake(320, 3, 360, 14)]autorelease];
    [viewMarqueeContainer setBackgroundColor:[UIColor clearColor]];
    [viewMarqueeContainer setClipsToBounds:YES];
    [viewMarqueeContainer setOpaque:YES];
 
    //內容
    UILabel *lblContent = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50,14)]autorelease];
    [lblContent setText:@"這裡是捲軸。。。。。"];
    [lblContent setTextColor:[UIColor whiteColor]];
    [lblContent setBackgroundColor:[UIColor clearColor]];
    [lblContent setFont:[UIFont fontWithName:@"Helvetica" size:12]];
    [lblContent setOpaque:YES];
 
    self.view= viewMarqueeBar;
    self.vMarqueeContainer = viewMarqueeContainer;
 
    [self.view addSubview:viewMarqueeContainer];
 
    [self marqueeView];
}

2.自行定義捲軸控制項,讓view自己滾動起來,通過不斷的相互方法調用實現迴圈滾動
UIMarqueeBarView.h定義

 代碼如下 複製代碼

 /**
 *UIMarqueeBarView.h
 */
@interface UIMarqueeBarView : UIView
{
}
 
- (void)start;
- (void)stop;
@end

UIMarqueeBarView.m實現

/**
 *UIMarqueeBarView.m
 */
 
#import "UIMarqueeBarView.h"
 
@implementation UIMarqueeBarView
 
- (void)dealloc
{
    [super dealloc];
}
 
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
  [self setupView];
    }
    return self;
}
 
-(id)initWithCoder:(NSCoder *)aDecoder {
 if( (self = [super initWithCoder:aDecoder]) ) {
  // Initialization code
  [self setupView];
 }
 return self;
}
 
- (void)setupView
{
    [self setBackgroundColor:[UIColor lightGrayColor]];
    [self setClipsToBounds:YES];
    [self setOpaque:YES];
 
    UILabel *lblContent = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150 ,16)]autorelease];
    [lblContent setText:@"這裡是捲軸。。。。。"];
    [lblContent setTextColor:[UIColor whiteColor]];
    [lblContent setBackgroundColor:[UIColor clearColor]];
    [lblContent setFont:[UIFont fontWithName:@"Helvetica" size:14]];
    [lblContent setNumberOfLines:1];
    [lblContent setOpaque:YES];
 
    [self addSubview:lblContent];
}
 
- (void)start
{
    if (self.viewContainer == NULL) {
        [self setupView];
    }
 
    [self startAnimation];
}
- (void)stop
{
 
}
 
-(void)startAnimation
{
    [UIView beginAnimations:@"MarqueeBarAniamation" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDuration:25];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
 
    CGRect viewFrame = self.viewContainer.frame;
    viewFrame.origin.x = -320;
    [self.viewContainer setFrame:viewFrame];
 
    [UIView commitAnimations];
}
 
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context

    CGRect viewFrame = self.viewContainer.frame;
    viewFrame.origin.x = 320;
    [self.viewContainer setFrame:viewFrame];
 [self startAnimation];
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.