//
// ViewController.m
// UIScrollVIew
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;//已经设置大小是300*200
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView1;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView2;
@end
@implementation ViewController
- (void)viewDidLoad {//控制器类加载完毕后
[super viewDidLoad];
//1、
UIView *redView = [[UIView alloc]init];
redView.backgroundColor = [UIColor redColor];
redView.frame = CGRectMake(0, 0, 50, 50);
[self.scrollView addSubview:redView];
self.scrollView.clipsToBounds = YES;
//设置内容尺寸,水平不能滚动,垂直滚动10,
self.scrollView.contentSize = CGSizeMake(300, 210);
//self.scrollView.scrollEnabled = NO; 不可以滚动
//是否能够跟用户交互
self.scrollView.userInteractionEnabled = YES;
[self.scrollView.subviews.firstObject removeFromSuperview];//移除
NSLog(@"%@",self.scrollView.subviews);
/* 另外2个是滚动条,
(
"<UIView: 0x7fae0340c7d0; frame = (0 0; 50 50); layer = <CALayer: 0x600000036740>>",
"<UIImageView: 0x7fae035126b0; frame = (294.667 190; 2.33333 7); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x608000030f60>> - (null)",
"<UIImageView: 0x7fae03512090; frame = (290 194.667; 7 2.33333); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x608000030da0>> - (null)"
)
*/
//2、加载大图片
UIImageView* image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"7"]];//大图片
self.scrollView1.contentSize = CGSizeMake(image.frame.size.width, image.frame.size.height);
self.scrollView1.bounces = YES;//弹簧效果
self.scrollView1.alwaysBounceVertical = YES;//没有设置contentSize的时候,不能滚动,但是有时候网络加载的时候,没有数据,需要下拉刷新来加载数据,就用这个,
self.scrollView1.alwaysBounceHorizontal = YES;
self.scrollView1.showsVerticalScrollIndicator = YES;//是否显示滚动条
[self.scrollView1 addSubview:image];
self.scrollView1.contentInset = UIEdgeInsetsMake(10, 20, 30, 40);//内边距
//self.scrollView1.contentOffset = CGPointMake(700, 700);//设置偏移
NSLog(@"%f",self.scrollView1.contentOffset.x);//获取滚动的偏移量
//3、下拉刷新,显示菊花。
UIActivityIndicatorView *iv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
iv.center = CGPointMake(100, -30);
[iv startAnimating];
self.scrollView2.backgroundColor = [UIColor yellowColor];
self.scrollView2.alwaysBounceVertical = YES;//没有设置contentSize不能滚动,为了下载加载并显示菊花。(不能滚动,只是有弹簧效果)
self.scrollView2.alwaysBounceHorizontal = NO;
[self.scrollView2 addSubview:iv];
//代理
UIScrollView *s = [[UIScrollView alloc]init];
s.backgroundColor = [UIColor redColor];
s.frame = CGRectMake(0, 20, 100, 100);
[self.view addSubview:s];
UIImage * i = [UIImage imageNamed:@"car"];
UIImageView* imagev = [[UIImageView alloc]initWithImage:i];
[s addSubview:imagev];
s.contentSize = i.size;
s.delegate = self;//代理,@property(nullable,nonatomic,weak) id<UIScrollViewDelegate> delegate; 任何类型但是必须遵守UIScrollViewDelegate协议,在控制器加上遵守协议,@interface ViewController ()<UIScrollViewDelegate>,协议不写在.h文件中,因为.h文件暴露在外面给别人看的。
}
//点击控制器view的空白处调用
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//self.scrollView1.contentOffset.y = 0;//不能直接修改oc对象的结构体变量的成员属性
//点击回到顶部,并且添加动画
[UIView animateWithDuration:2.0 animations:^{
CGPoint cp = self.scrollView1.contentOffset;
cp.y = 0;
self.scrollView1.contentOffset = cp;
}];
//点击回到左边
[self.scrollView1 setContentOffset:CGPointMake(0, self.scrollView1.contentOffset.y) animated:YES];
NSLog(@"dddd");
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView //区域正在滚动时候调用
{
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView //开始滚动时候调用
{
}
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset//即将停止拖拽时候调用
{
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView//减速完毕调用
{
}
@end