scrollViewf 動態滑動的廣告欄

//頭文件:

//
//  QiuyAdvertiseVController.h
//  QiuyAdvertiseVController
//
//  Created by laizhenjie on 14-3-20.
//  Copyright (c) 2014年 Laizhenjie. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface QiuyAdvertiseVController : UIViewController

@property(nonatomic, strong) NSMutableArray *advArray;
@property(nonatomic, strong) UIScrollView *advScrliew;
@property(nonatomic, strong) UIPageControl *advPageCtrl;
@property(nonatomic, strong) NSTimer *timeToNextPage;


@end


//實現文件:


//
//  QiuyAdvertiseVController.m
//  QiuyAdvertiseVController
//
//  Created by laizhenjie on 14-3-20.
//  Copyright (c) 2014年 Laizhenjie. All rights reserved.
//

#import "QiuyAdvertiseVController.h"

#define KADV_SCRLVIEW_H 122
#define KADV_SCRLVIEW_W 320

#define KADV_PAGECTRL_H 22
#define KTIME_INTEVAL 2

@interface QiuyAdvertiseVController ()<UIScrollViewDelegate>

@end

@implementation QiuyAdvertiseVController

@synthesize advArray;
@synthesize advScrliew;
@synthesize advPageCtrl;
@synthesize timeToNextPage;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initAdvscrlView];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


//===================================================
- (void)initAdvscrlView{

    advArray = [[NSMutableArray alloc]initWithObjects:@"adv11.png",@"adv12.png",@"adv11.png", nil];
    advScrliew = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 32, KADV_SCRLVIEW_W, KADV_SCRLVIEW_H)];
    [advScrliew setDelegate:self];
    [advScrliew setPagingEnabled:YES];
    [advScrliew setBackgroundColor:[UIColor brownColor]];
    [self.view addSubview:advScrliew];
    
    //PAGECTRL
    advPageCtrl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, advScrliew.frame.size.height + advScrliew.frame.origin.y, advScrliew.frame.size.width, KADV_PAGECTRL_H)];
    
    [advPageCtrl setBackgroundColor:[UIColor yellowColor]];
    advPageCtrl.pageIndicatorTintColor = [UIColor blackColor];
    [advPageCtrl setCurrentPageIndicatorTintColor:[UIColor brownColor]];
    advPageCtrl.numberOfPages = 0;
    [self.view addSubview:advPageCtrl];
    
    [self reloadAdvScrlViews];
    
}

#pragma  mark 加載廣告子視圖
-(void)reloadAdvScrlViews
{
    
    for(UIView* subview in advScrliew.subviews)
    {
        [subview removeFromSuperview];
    }
    
    NSInteger pageNumber = [advArray count];
    advScrliew.contentSize = CGSizeMake(KADV_SCRLVIEW_W * pageNumber, KADV_SCRLVIEW_H);
    for(uint i=0; i<pageNumber; i++)
    {
        [advScrliew addSubview:[self getPageView:i]];
    }
    advPageCtrl.numberOfPages = pageNumber;
}



#pragma  mark 新建子視圖
- (UIView*)getPageView:(NSInteger)pageId
{
    UIImageView* pageView = [[UIImageView alloc] initWithFrame:CGRectMake(KADV_SCRLVIEW_W * pageId,0, KADV_SCRLVIEW_W, KADV_SCRLVIEW_H)];
//    pageView.autoresizingMask = UIViewAutoresizingNone;
//    pageView.contentMode = UIViewContentModeScaleToFill;
    pageView.image = [UIImage imageNamed:[advArray objectAtIndex:pageId]];
    
    return pageView;
}


#pragma mark SCRLVIEW DELEGATE 
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSLog(@"contentoffset.x:%f",scrollView.contentOffset.x);
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    advPageCtrl.currentPage = page;
}

#pragma mark MOVETONEXT
- (void)moveToNextPage:(NSTimer*)timer{
    
    NSLog(@"moveToNext.");
    if(advPageCtrl.currentPage == advPageCtrl.numberOfPages-1)
    {
        advPageCtrl.currentPage = 0;
    }
    else {
        advPageCtrl.currentPage++;
    }
    
    CGRect frame = advScrliew.frame;

    frame.origin.x = frame.size.width * advPageCtrl.currentPage;
    [advScrliew scrollRectToVisible:frame animated:YES];
    
}


#pragma mark - TIME TO NEXT PAGE
- (void)viewWillAppear:(BOOL)animated{
    
    timeToNextPage = [NSTimer scheduledTimerWithTimeInterval:KTIME_INTEVAL target:self selector:@selector(moveToNextPage:) userInfo:nil repeats:YES];
    
}



- (void)viewDidDisappear:(BOOL)animated{
    if(timeToNextPage){
        [timeToNextPage invalidate];
        self.timeToNextPage = nil;
    }
    
}



@end


發佈了53 篇原創文章 · 獲贊 4 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章