UIScrollView
UIScrollView基本用法和代理方法
- (void)viewDidLoad{ [super viewDidLoad]; scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; scrollView.backgroundColor = [UIColor redColor]; // 是否支持滑动最顶端// scrollView.scrollsToTop = NO; scrollView.delegate = self; // 设置内容大小 scrollView.contentSize = CGSizeMake(320, 460*10); // 是否反弹// scrollView.bounces = NO; // 是否分页// scrollView.pagingEnabled = YES; // 是否滚动// scrollView.scrollEnabled = NO;// scrollView.showsHorizontalScrollIndicator = NO; // 设置indicator风格// scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite; // 设置内容的边缘和Indicators边缘// scrollView.contentInset = UIEdgeInsetsMake(0, 50, 50, 0);// scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 50, 0, 0); // 提示用户,Indicators flash [scrollView flashScrollIndicators]; // 是否同时运动,lock scrollView.directionalLockEnabled = YES; [self.view addSubview:scrollView]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 320, 40)]; label.backgroundColor = [UIColor yellowColor]; label.text = @"学习scrolleview"; [scrollView addSubview:label]; [label release];}#pragma mark - /*// 返回一个放大或者缩小的视图- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{ }// 开始放大或者缩小- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{ }// 缩放结束时- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{ } // 视图已经放大或缩小- (void)scrollViewDidZoom:(UIScrollView *)scrollView{NSLog(@"scrollViewDidScrollToTop");} */// 是否支持滑动至顶部- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{ return YES;}// 滑动到顶部时调用该方法- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{ NSLog(@"scrollViewDidScrollToTop");}// scrollView 已经滑动- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ NSLog(@"scrollViewDidScroll");}// scrollView 开始拖动- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ NSLog(@"scrollViewWillBeginDragging");}// scrollView 结束拖动- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ NSLog(@"scrollViewDidEndDragging");}// scrollView 开始减速(以下两个方法注意与以上两个方法加以区别)- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{ NSLog(@"scrollViewWillBeginDecelerating");}// scrollview 减速停止- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ NSLog(@"scrollViewDidEndDecelerating"); }@interface RootViewController : UIViewController<UIScrollViewDelegate> { UIScrollView *_scrollView; NSMutableArray *slideImages; UIPageControl *_page; } @end #import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, 320, 240)]; _scrollView.bounces = NO; _scrollView.pagingEnabled = YES; _scrollView.delegate = self; _scrollView.contentOffset = CGPointMake(320, 0); _scrollView.contentSize = CGSizeMake(1920,240); _scrollView.showsVerticalScrollIndicator =NO; _scrollView.showsHorizontalScrollIndicator = NO; _scrollView.userInteractionEnabled = YES; [self.view addSubview:_scrollView]; [_scrollView release]; slideImages = [[NSMutableArray alloc]initWithObjects:@"1.jpeg",@"2.jpg",@"3.jpeg",@"4.jpeg", nil]; UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:([slideImages count]-1)]]]; imageView.frame = CGRectMake(0, 0, 320, 240); [_scrollView addSubview:imageView]; [imageView release]; for (int i = 0;i<[slideImages count];i++) { //loop this bit UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:i]]]; imageView.frame = CGRectMake(320*i+320, 0, 320, 240); imageView.userInteractionEnabled = YES; [_scrollView addSubview:imageView]; [imageView release]; } imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:0]]]; imageView.frame = CGRectMake(320*5, 0, 320, 240); [_scrollView addSubview:imageView]; [imageView release]; _page = [[UIPageControl alloc]initWithFrame:CGRectMake(240, 230, 70, 30)]; _page.numberOfPages = 4; _page.currentPage = 0; // _page.backgroundColor = [UIColor grayColor]; [_page addTarget:self action:@selector(pageAction) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_page]; [_page release]; // Do any additional setup after loading the view. } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { int currentPage = (_scrollView.contentOffset.x - _scrollView.frame.size.width / ([slideImages count]+2)) / _scrollView.frame.size.width + 1; NSLog(@"%d",currentPage); if (currentPage==0) { [_scrollView scrollRectToVisible:CGRectMake(320*4, 0, 320, 240) animated:NO]; } else if (currentPage==([slideImages count]+1)) { //如果是最后+1,也就是要开始循环的第一个 [_scrollView scrollRectToVisible:CGRectMake(320, 0, 320, 240) animated:NO]; } } - (void)scrollViewDidScroll:(UIScrollView *)sender { int page = _scrollView.contentOffset.x/320-1; _page.currentPage = page; } -(void)pageAction { int page = _page.currentPage; [_scrollView setContentOffset:CGPointMake(320 * (page+1), 0)]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)dealloc { [slideImages release]; [super dealloc]; } @endposted on 2015-11-05 11:46 阿斯顿发-斯语iOS 阅读(148) 评论(0) 收藏 举报
浙公网安备 33010602011771号