您的位置:首页 > 其它

scrollViewf 动态滑动的广告栏

2014-03-20 13:55 218 查看
//头文件:

//
//  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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: