1.什么是UIScrollView:
// scrollView对象当前滚动到contentOffset 位置 @property(nonatomic) CGPoint contentOffset; // default CGPointZero // scrollView对象可以滚动的范围大小 @property(nonatomic) CGSize contentSize; // default CGSizeZero // scrollView对象在top,left,bottom,right四个边上额外增加的滚动区域 @property(nonatomic) UIEdgeInsets contentInset; // default UIEdgeInsetsZero. add additional scroll area around content // scrollView对象的代理,用动兼听该scrollView对象 @property(nullable,nonatomic,weak) id<UIScrollViewDelegate> delegate; // default nil. weak reference // 设置UIScrollView是否需要弹簧效果 @property(nonatomic) BOOLbounces; // 设置UIScrollView是否能滚动 @property(nonatomic,getter=isScrollEnabled)BOOL scrollEnabled; // 是否显示水平滚动条 @property(nonatomic) BOOL showsHorizontalScrollIndicator; // 是否显示垂直滚动条 @property(nonatomic) BOOL showsVerticalScrollIndicator; // 视图最小缩放比例 @property(nonatomic) CGFloat minimumZoomScale; // default is 1.0 // 视图最大缩放比例 @property(nonatomic) CGFloat maximumZoomScale; // default is 1.0. must be > minimum zoom scale to enable zooming
4.scrollView对象代理delegate:
》要成为delegate,那么就得遵守UIScrollViewDelegate协议,然后实现协议中相应的方法和指定一个代理对象,就可以监听UIScrollView的滚动过程了。
》监听过程:当UIScrollView对象被滚动时,就会自动调用代理delegate对象己实现的确定方法。
》代理常用方法:
//scrollView对象正在滚动时调用该方法 - (void)scrollViewDidScroll:(UIScrollView *)scrollView; // scrollView对象在被拖动前一刻调用该方法 // called on start of dragging (may require some time and or distance to move) - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView; // 用户结束拖动时调用 // called on finger up if the user dragged. decelerate is true if it will continue moving afterwards - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate; //当用户在UIScrollView身上使用捏合手势时,UIScrollView会调用代理的viewForZoomingInScrollView:方法,这个方法返回的控件就是需要进行缩放的控件 - (nullableUIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView; // return a view that will be scaled. if delegate returns nil, nothing happens
5.如果UIScrollView无法滚动,可能是以下原因:
- (UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView;
-(void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
-(void)scrollViewDidZoom:(UIScrollView *)scrollView
//一共有多少页 @property(nonatomic)NSInteger numberOfPages; //当前显示的页码 @property(nonatomic)NSInteger currentPage; //只有一页时,是否需要隐藏页码指示器 @property(nonatomic)BOOL hidesForSinglePage; //其他页码指示器的颜色 @property(nonatomic,retain)UIColor *pageIndicatorTintColor; //当前页码指示器的颜色 @property(nonatomic,retain)UIColor *currentPageIndicatorTintColor;
+ (NSTimer*)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
每隔ti秒,调用一次aTarget的aSelector方法,yesOrNo决定了是否重复执行这个任务
- (void)invalidate;
9.实例:
// // ViewController.m // 05-西马拉牙 // // Created by 瞿杰 on 15/9/25. // #import "ViewController.h" #define AD_BAR_IMG_NUMBER 5 @interface ViewController ()<UIScrollViewDelegate> @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @property (weak, nonatomic) IBOutlet UIScrollView *adBarScrollView; @property (weak, nonatomic) UIPageControl * pageControl; - (void)addAdBarImgView; - (void)addPageControl; - (void)nextImgView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //设置缩放 self.scrollView.delegate = self; self.scrollView.minimumZoomScale = 0.5; self.scrollView.maximumZoomScale = 2.0; // self.scrollView.bouncesZoom = YES ; CGSize size = self.view.frame.size; self.scrollView.contentSize = CGSizeMake(size.width, size.height + 50); self.scrollView.contentOffset = CGPointMake(0, -100); // self.scrollView.contentInset = UIEdgeInsetsMake(10, 20, 30, 40); [self addAdBarImgView]; [self addPageControl]; // 定时器 [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(nextImgView) userInfo:nil repeats:YES]; } /** scrollView代理对象方法,用于反回一个需要缩放的视图控件*/ - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{ return [[scrollView subviews] objectAtIndex:0] ; } /** 添加广告栏图片 */ - (void)addAdBarImgView{ int numberBar = AD_BAR_IMG_NUMBER; CGFloat adBarImgW = self.adBarScrollView.frame.size.width ; CGFloat adBarImgH = self.adBarScrollView.frame.size.height ; CGFloat adBarImgY = 0 ; for (int i = 0; i < numberBar; i++) { UIImageView * adBarImg = [[UIImageView alloc]init]; CGFloat adBarImgX = i * adBarImgW ; adBarImg.frame = CGRectMake(adBarImgX, adBarImgY, adBarImgW, adBarImgH); [adBarImg setImage:[UIImage imageNamed:[NSString stringWithFormat:@"img_%02d",i+1]]]; [self.adBarScrollView addSubview:adBarImg]; } self.adBarScrollView.contentSize = CGSizeMake(numberBar * adBarImgW, adBarImgH); self.adBarScrollView.showsHorizontalScrollIndicator = NO; // self.adBarScrollView.pagingEnabled = YES ; } - (void)addPageControl{ UIPageControl * pageControl = [[UIPageControl alloc]init]; CGFloat pageW = 100 ; CGFloat pageH = 20 ; CGFloat pageX = (self.adBarScrollView.frame.size.width - pageW )/2; CGFloat pageY = self.adBarScrollView.frame.size.height - pageH ; pageControl.frame = CGRectMake( pageX, pageY, pageW, pageH); pageControl.numberOfPages = AD_BAR_IMG_NUMBER ; pageControl.currentPageIndicatorTintColor = [UIColor redColor]; pageControl.pageIndicatorTintColor = [UIColor blueColor]; self.pageControl = pageControl ; [self.view addSubview:pageControl]; } //定时器每隔 1 秒调用方法一次,使得图片被循环显示 - (void)nextImgView{ // NSLog(@"-------%ld",self.pageControl.currentPage); if(self.pageControl.currentPage == AD_BAR_IMG_NUMBER -1) self.pageControl.currentPage = 0 ; else self.pageControl.currentPage++; self.adBarScrollView.contentOffset = CGPointMake(self.pageControl.currentPage * self.adBarScrollView.frame.size.width, 0); } @end