UIScrollView的基本用法和简单示例
第一、UIScrollView的基本概念是一个可以选择滑动的视图,用于显示更多的内容,可以通过手势放大或者缩小显示更多的内容。
有两个子类一个是UITableView,另一个是UITextView
第二、基本属性:
scrollView=[[[UIScrollViewalloc] initWithFrame:CGRectMake(0,0, 320,250)] autorelease];
scrollView.backgroundColor=[UIColorredColor];
//设置内容的大小
scrollView.contentSize=CGSizeMake(320*4,250);
//当超出边界时表示是否可以反弹
scrollView.bounces=YES;
//是否分页
scrollView.pagingEnabled=YES;
//是否滚动
scrollView.scrollEnabled=YES;
//是否显示边界
scrollView.showsHorizontalScrollIndicator=YES;
//设置indicator的风格
scrollView.indicatorStyle=UIScrollViewIndicatorStyleWhite;
//提示用户
[scrollViewflashScrollIndicators];
//设置内容的边缘
scrollView.contentInset=UIEdgeInsetsMake(0,50, 50,0);
[self.viewaddSubview:scrollView];
UILabel *label=[[[UILabelalloc] initWithFrame:CGRectMake(320,210, 320,40)] autorelease];
label.backgroundColor=[UIColoryellowColor];
label.text=@"学习scroolView";
[scrollViewaddSubview:label];
//调转到指定的offset
[scrollViewsetContentOffset:CGPointMake(320,0)];
第三、利用UIPageController和UIScrollView实现滚动图片
- (void)viewDidLoad { [super viewDidLoad]; UIScrollView *ScrollView=[[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)] autorelease]; ScrollView.delegate=self; ScrollView.scrollsToTop=YES; ScrollView.contentSize=CGSizeMake(320*5, 180); ScrollView.backgroundColor=[UIColor clearColor]; ScrollView.pagingEnabled=YES; ScrollView.showsHorizontalScrollIndicator=NO; self.tableView.tableHeaderView=ScrollView; for (int index=0; index<5; index++) { UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(index*320, 0, 320, 180)]; imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"image%d.jpg",index+1]]; [ScrollView addSubview:imageView]; [imageView release]; } UIPageControl *pageControl=[[[UIPageControl alloc] initWithFrame:CGRectMake(0, 150, 320, 30)] autorelease]; [self.tableView addSubview:pageControl]; pageControl.numberOfPages=5; pageControl.tag=1001; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell==nil) { cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } cell.textLabel.text=[NSString stringWithFormat:@"row: %d",indexPath.row]; return cell; } #pragma mark -UIScrollViewDelegate - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView { if ([scrollView isMemberOfClass:[UITableView class]]) { NSLog(@"HELLO"); }else { int current=scrollView.contentOffset.x/320; NSLog(@"scrollview :%f",scrollView.contentOffset.x); UIPageControl *pageControl=(UIPageControl*)[self.view viewWithTag:1001]; pageControl.currentPage=current; } }
补充:综合编程 , 其他综合 ,