知识的总结
2015-09-28 19:03 真实16 阅读(147) 评论(0) 编辑 收藏 举报imageArray = @[@"长城3",@"长城1",@"长城2",@"长城景色"];
// ----------添加滚动视图
myscrollView = [[UIScrollView alloc]init];
myscrollView.frame = tableView.tableHeaderView.frame;
myscrollView.delegate = self;
// 是否允许分页
myscrollView.pagingEnabled = YES;
// 不能垂直的移动
myscrollView.showsVerticalScrollIndicator = NO;
myscrollView.contentSize = CGSizeMake(CGRectGetWidth(tableView.tableHeaderView.frame)*imageArray.count, 200);
[tableView.tableHeaderView addSubview:myscrollView];
for (int i = 0; i < imageArray.count; i++) {
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH*i,0 , CGRectGetWidth(tableView.tableHeaderView.frame), HEIGHT/3)];
imageView.image= [UIImage imageNamed:imageArray[i]];
// 在滚动视图上添加图片
[myscrollView addSubview:imageView];
};
// 建立页面控制视图
pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(WIDTH/4+40, HEIGHT/4, 100, 40)];
// 表示的是几张图片
pageControl.numberOfPages = imageArray.count;
pageControl.currentPage = 0;
// 变动时的颜色
pageControl.pageIndicatorTintColor = [UIColor greenColor];
// 现在的颜色
pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
pageControl.tag = 100;
// 必须加载在View上,而不是滚动视图上
[tableView.tableHeaderView addSubview:pageControl];
[self addTimer];
//添加定时器
- (void)addTimer
{
timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:selfselector:@selector(nextImage) userInfo:nil repeats:YES];
// 记住这段代码,定时器上表示一定动
[[NSRunLoop currentRunLoop] addTimer:timerforMode:NSRunLoopCommonModes];
}
//移除定时器
- (void)removeTimer
{
// invalidate |ɪnˈvælɪdeɪt| 使什么作废
[timer invalidate];
// 时间为空
timer = nil;
}
// 定时器调用方法
- (void)nextImage
{
// 1.增加pageControl的页码
if (myscrollView.contentOffset.x ==0) {
CGFloat x = WIDTH*(pageControl.currentPage+1);
// 当前的页数
NSInteger curPage = x/WIDTH;
// 表示的是BOOL last()
if (last == YES&&pageControl.currentPage>=imageArray.count-1) {
last = NO;
pageControl.currentPage = 0;
myscrollView.contentOffset = CGPointMake(0, 0);
}else{
pageControl.currentPage = curPage;
if (pageControl.currentPage == imageArray.count-1) {
last = YES;
}
}
}else{
CGFloat x = myscrollView.contentOffset.x+WIDTH*pageControl.currentPage;
// NSLog(@"%f",scrollShowTeacher.contentOffset.x);
NSInteger curPage = x/WIDTH;
if (last == YES&&pageControl.currentPage>=imageArray.count-1) {
last = NO;
pageControl.currentPage = 0;
myscrollView.contentOffset = CGPointMake(0, 0);
}else{
pageControl.currentPage = curPage;
if (pageControl.currentPage == imageArray.count-1) {
last = YES;
}
}
}
// ------------------------------------------------
// 2.计算scrollView滚动的位置
CGFloat offsetX = pageControl.currentPage * WIDTH;
CGPoint offset = CGPointMake(offsetX, 0);
// NSLog(@"------");
[myscrollView setContentOffset:offset animated:YES];
}
//开始拖拽的时候移除定时器
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self removeTimer];
}
//停止拖拽的时候添加定时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
[self addTimer];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// 滚动尺寸的偏移量
CGFloat x = scrollView.contentOffset.x;
// NSLog(@"%f",x);
// teacherPage.currentPage = x/w;
// 当前的页面的数量
NSInteger curPage = x/WIDTH;
if (last == YES&&pageControl.currentPage>imageArray.count-1) {
last = NO;
pageControl.currentPage = 0;
// 滚动视图的偏移量
scrollView.contentOffset = CGPointMake(0, 0);
}else{
pageControl.currentPage = curPage;
if (pageControl.currentPage == imageArray.count-1) {
last = YES;
}
}
}
设置UISearchBar(收索栏) 在当前的也页面显示,到跳转到下一个页面,UISearchBar(收索栏)就消失
#pragma mark ----------隐藏工具栏
- (BOOL)prefersStatusBarHidden
{
return YES;
}
#pragma mark ------搜索栏(回收键盘)
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
}
#pragma mark -----------让收索栏出现
-(void)viewWillAppear:(BOOL)animated
{
[self.navigationController.navigationBar viewWithTag:100].hidden = NO;
}
【UISearchBar的代理方法】
#pragma mark ------------让收索栏消失
- (void)viewDidDisappear:(BOOL)animated
{
[self.navigationController.navigationBar viewWithTag:100].hidden = YES;
}