#import "ViewController.h"
#define KScreenW [UIScreen mainScreen].bounds.size.width
#define KScreenH [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/*——————————————————————————UIScrollView 滑动视图————————————————————————————————————————————————————-*/
//1.创建
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, KScreenW, KScreenW)];
scrollView.backgroundColor = [UIColor grayColor];
[self.view addSubview:scrollView];
//2.
//1.尺寸
scrollView.contentSize = CGSizeMake(5 * KScreenW, KScreenW);
//2.是否开启分页 默认NO
scrollView.pagingEnabled = YES;
//3.是否开启滚动条 默认YES
//水平
scrollView.showsHorizontalScrollIndicator = YES;
//垂直
scrollView.showsVerticalScrollIndicator = NO;
//4.滑动条样式
/**
*
UIScrollViewIndicatorStyleDefault, // black with white border. good against any background 系统默认滑动条
UIScrollViewIndicatorStyleBlack, // black only. smaller. good against a white background 黑色的滑动条
UIScrollViewIndicatorStyleWhite // white only. smaller. good against a black background 白色的滑动条
*/
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
//5.是否开启首位反弹效果 默认YES
//滑动
scrollView.bounces = YES;
//缩放
scrollView.bouncesZoom = YES;
//6.是否开启滚动 默认YES
scrollView.scrollEnabled = YES;
//7.item的偏移量
// scrollView.contentInset = UIEdgeInsetsMake(0, 50, 0, 50);
//8.content 的偏移量
// scrollView.contentOffset = CGPointMake(2*KScreenW, 0);
//两秒后才偏移
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
// [scrollView setContentOffset:CGPointMake(2 *KScreenW, 0) animated:YES];
// });
//8.设置代理对象,(※※※)重要
scrollView.delegate = self;
//获取图片
for (int i = 0; i < 5; i++) {
//获取照片
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"scene%d.jpg",i+1]];
//imageView的大小
UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(i* KScreenW, 0, KScreenW, KScreenW)];
//把图片放入图片视图
imageV.image = image;
//把图片视图放入scrollView里
[scrollView addSubview:imageV];
}
}
#define mark --UIScrollViewDelegate
//将要开始拖动时
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"将要开始拖动");
}
//正在滑动中
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//滑动中x表示在整个视图中的位置
// NSLog(@"x -> %f",scrollView.contentOffset.x);
}
//将要结束拖动时 手指离开屏幕
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout nonnull CGPoint *)targetContentOffset{
NSLog(@"%@",NSStringFromCGPoint(velocity));
NSLog(@"%f %f",targetContentOffset->x,targetContentOffset->y);
NSLog(@"将要结束拖动时");
}
//已经结束拖动时
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
if (decelerate == YES) {
NSLog(@"正在减速");
}
NSLog(@"已经结束拖动");
}
//将要开始减速时
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
NSLog(@"将要开始减速");
}
//已经结束减速时
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@"已经结束减速");
}
//已经结束滑动动画时
- (void)scrollViewDidEndScrollingAnination:(UIScrollView *)scrollView{
NSLog(@"已经结束滑动动画时");
}
////滑动到顶部
//- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{
//
// NSLog(@"top");
// return YES;
//}
////已经滑动到顶部
//- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
//
// NSLog(@"已经滑动到顶部");
//}
//缩放时调用
//- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
//}
//返回一个将要缩放的视图
//- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
// return nil;
//}
//将要开始缩放时
//- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view{}
//已经结束缩放时
//- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale{}
@end
2016-03-04 17:14:15.579 自定义分页[2549:256434] 将要开始拖动
2016-03-04 17:14:16.290 自定义分页[2549:256434] {0, 0}
2016-03-04 17:14:16.290 自定义分页[2549:256434] 0.000000 0.000000
2016-03-04 17:14:16.291 自定义分页[2549:256434] 将要结束拖动时
2016-03-04 17:14:16.291 自定义分页[2549:256434] 正在减速
2016-03-04 17:14:16.291 自定义分页[2549:256434] 已经结束拖动
2016-03-04 17:14:16.292 自定义分页[2549:256434] 将要开始减速
2016-03-04 17:14:16.678 自定义分页[2549:256434] 已经结束减速
Day.03.04 UIScrollView 滑动代理方法
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- ![Uploading 屏幕快照 2016-03-04 下午7.21.33_759992.png . . .]