关于UIScrollViewDelegate协议中每个回调函数的

@protocol UIScrollViewDelegate<NSObject>

@optional

- (void)scrollViewDidScroll:(UIScrollView *)scrollView;   // 只要view有滚动(不管是拖、拉、放大、缩小   等导致) 都会执行此函数---any offset changes

- (void)scrollViewDidZoom:(UIScrollView *)scrollView __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_2); // view的缩放,参考另一篇博文---any zoom scale changes

// called on start of dragging (may require some time and or distance to move)
// 将要开始拖拽,手指已经放在view上并准备拖动的那一刻
- (void)scrollViewWillBeginDragg ing:(UIScrollView *)scrollView;

// called on finger up if the user dragged. velocity is in points/second. targetContentOffset may be changed to adjust where the scroll view comes to rest. not called when pagingEnabled is YES
// 将要结束拖拽,手指已拖动过view并准备离开手指的那一刻   注意:当属性pagingEnabled为YES时此函数不被调用
- (void)scrollViewWillEndDraggin g:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);

// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
// 已经结束拖拽,手指刚离开view的那一刻
- (void)scrollViewDidEndDragging :(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;

// called on finger up as we are moving
//   view将要开始减速 view滑动之后有惯性 
- (void)scrollViewWillBeginDecel erating:(UIScrollView *)scrollView;

// called when scroll view grinds to a halt
// view已经停止滚动 
- (void)scrollViewDidEndDecelera ting:(UIScrollView *)scrollView;         

// called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating
// 有动画时调用
- (void)scrollViewDidEndScrollin gAnimation:(UIScrollView *)scrollView;

// return a view that will be scaled. if delegate returns nil, nothing happens
// 直接看英文吧
- (UIView *)viewForZoomingInScrollVi ew:(UIScrollView *)scrollView;   

// called before the scroll view begins zooming its content 
- (void)scrollViewWillBeginZoomi ng:(UIScrollView *)scrollView withView:(UIView *)view __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_2);

  // scale between minimum and maximum. called after any 'bounce' animations
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale;

// return a yes if you want to scroll to the top. if not defined, assumes YES
- (BOOL)scrollViewShouldScrollTo Top:(UIScrollView *)scrollView;

// called when scrolling animation finished. may be called immediately if already at top 
- (void)scrollViewDidScrollToTop :(UIScrollView *)scrollView;         

@end

执行顺序:
willBeginDragging
DidScroll
willEndDragging
DidEndDragging
DidScroll
willBeginDecelerating
DidScroll
DidEndDecelerating

#define DELEGATE_CALLBACK(DELEGATE, SEL) if (DELEGATE && [DELEGATE respondsToSelector:@selector(SEL)]) [DELEGATE performSelector:@selector(SEL)]
#define DELEGATE_CALLBACK_ONE_PARAMETER(DELEGATE, SEL, X) if (DELEGATE && [DELEGATE respondsToSelector:@selector(SEL)]) [DELEGATE performSelector:@selector(SEL) withObject:X]
#define DELEGATE_CALLBACK_TWO_PARAMETER(DELEGATE, SEL, X, Y) if (DELEGATE && [DELEGATE respondsToSelector:@selector(SEL)]) [DELEGATE performSelector:@selector(SEL) withObject:X withObject:Y]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用 Objective-C 纯代码编写的轮播图示例: 1. 创建一个 UIScrollView 对象和一个 UIPageControl 对象: ```objective-c @interface ViewController () <UIScrollViewDelegate> @property (nonatomic, strong) UIScrollView *scrollView; @property (nonatomic, strong) UIPageControl *pageControl; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 初始化 scrollView self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)]; self.scrollView.delegate = self; self.scrollView.pagingEnabled = YES; self.scrollView.showsHorizontalScrollIndicator = NO; [self.view addSubview:self.scrollView]; // 初始化 pageControl self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 180, self.view.bounds.size.width, 20)]; self.pageControl.currentPageIndicatorTintColor = [UIColor redColor]; self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor]; [self.view addSubview:self.pageControl]; } @end ``` 2. 在 scrollView 添加图片: ```objective-c - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; // 添加图片到 scrollView NSArray *imageNames = @[@"image1", @"image2", @"image3", @"image4"]; CGFloat width = self.scrollView.bounds.size.width; CGFloat height = self.scrollView.bounds.size.height; for (int i = 0; i < imageNames.count; i++) { UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * width, 0, width, height)]; imageView.image = [UIImage imageNamed:imageNames[i]]; [self.scrollView addSubview:imageView]; } // 设置 scrollView 的 contentSize self.scrollView.contentSize = CGSizeMake(width * imageNames.count, height); } ``` 3. 实现 scrollView 的滚动和 pageControl 的更新: ```objective-c - (void)scrollViewDidScroll:(UIScrollView *)scrollView { // 更新 pageControl 的当前页码 NSInteger currentPage = (NSInteger)(scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5); self.pageControl.currentPage = currentPage; } ``` 4. 最后,为了让轮播图自动滚动,可以使用定时器来实现: ```objective-c - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // 定时器自动滚动 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; } - (void)nextPage { // 计算下一页的页码 NSInteger nextPage = (self.pageControl.currentPage + 1) % self.pageControl.numberOfPages; CGPoint offset = CGPointMake(nextPage * self.scrollView.bounds.size.width, 0); [self.scrollView setContentOffset:offset animated:YES]; } ``` 以上就是一个简单的使用 Objective-C 纯代码编写的轮播图示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值