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
相关文章推荐
- 企业综合管理系统 BATE2.0 使用帮助(完整版)
- 严苛模式
- UI设计师不可不知的安卓屏幕知识
- 证书库中证书与密钥容器的关联
- 时间
- android requestWindowFeature()的应用
- Stanford机器学习---第五讲. 神经网络的学习 Neural Networks learning
- 抽风
- 最后能够用一段话介绍某一个娱乐城
- lucene + spring
- mysql实现自定义排序
- or1200开发环境的安装
- 5555555555555555555555555
- 44444444444444444444444
- 华为2014年机试样题 Word Maze
- Android gzip、base64 加密、解密
- Windows启动过程
- 333333333
- 222222222222222222222222
- android.graphics.Canvas类详解