1.设置滚动范围 contentSize
self.scrollView.contentSize = CGSizeMake(400, 250);
// 如果想禁止某个方向的滚动,那么就可以直接设置width或者height==0
self.scrollView.contentSize = CGSizeMake(400, 0);
2.contentOffset / UIView动画 block delegate
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"minion"]];
[self.scrollView addSubview:imageView];
// 设置内容大小
self.scrollView.contentSize = imageView.image.size;
}
- (IBAction)left:(id)sender {
// self.scrollView.contentOffset : 偏移量
// 记录UIScrollView滚动的位置,滚到哪
// 总结:内容的左上角 和 scrollView自身左上角 的 X\Y差值
// [UIView animateWithDuration:2.0 animations:^{
// self.scrollView.contentOffset = CGPointMake(0, self.scrollView.contentOffset.y);
// }];
[UIView animateWithDuration:2.0 animations:^{
self.scrollView.contentOffset = CGPointMake(0, self.scrollView.contentOffset.y);
} completion:^(BOOL finished) {
NSLog(@"执行完毕");
}];
}
- (IBAction)top:(id)sender {
// self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);
CGPoint offset = CGPointMake(self.scrollView.contentOffset.x, 0);
[self.scrollView setContentOffset:offset animated:YES];
}
- (IBAction)bottom:(id)sender {
[UIView animateWithDuration:2.0 animations:^{
CGFloat offsetY = self.scrollView.contentSize.height - self.scrollView.frame.size.height;
CGPoint offset = self.scrollView.contentOffset;
offset.y = offsetY;
self.scrollView.contentOffset = offset;
}];
}
- (IBAction)right:(id)sender {
[UIView beginAnimations:nil context: nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationDelegate:self]; // 代理
[UIView setAnimationDidStopSelector:@selector(stop)];
[UIView setAnimationWillStartSelector:@selector(start)];
CGFloat offsetX = self.scrollView.contentSize.width - self.scrollView.frame.size.width;
self.scrollView.contentOffset = CGPointMake(offsetX, self.scrollView.contentOffset.y);
[UIView commitAnimations];
}
- (void)start
{
NSLog(@"start");
}
- (void)stop
{
NSLog(@"stop");
}
@end
OC语法细节:不允许直接修改OC对象的结构体属性的成员
[V] self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, offsetY);
[X] self.scrollView.contentOffset.y = offsetY;
3.contentInset
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"minion"]];
// [self.scrollView addSubview:imageView];
[self.scrollView insertSubview:imageView atIndex:0];
// 设置内容大小
self.scrollView.contentSize = imageView.image.size;
// 设置contentInset
self.scrollView.contentOffset = CGPointMake(0, -64);
// UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
// 顶部额外增加 64 高度的可滚动范围
self.scrollView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
1-顶部额外增加64高度的可滚动范围.png
顶部额外增加64高度的可滚动范围,scrollView滚动后能停在右图位置,图片距离scrollView顶部有64高度空间
没有设置
self.scrollView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
2-图片顶部位于scrollView顶部.png
scrollView滚动后,图片顶部位于scrollView顶部,没有64高度的空间
4.UIScrollView滚动条
#import "ViewController.h"
@interface ViewController ()
{
CGRect _lastRect;
}
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property(nonatomic,strong) UIView *gird;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//self.scrollView.showsHorizontalScrollIndicator = NO;
//self.scrollView.showsVerticalScrollIndicator = NO;
NSMutableArray *girdArr = [NSMutableArray array];
for (int i = 0; i<50; i++) {
int row = i / 3; // 行号
int col = i % 3; // 列号
CGFloat x = col * (50 + 30); // 列号决定了X
CGFloat y = row * (50 + 30); // 行号决定了Y
self.gird = [self addGridWithX:x y:y];
if (i == 49) {
_lastRect = self.gird.frame;
}
[girdArr addObject:self.gird];
}
// self.scrollView.subviews[49];
UIView *lastView = [girdArr lastObject];
// UIView *lastView = [self.scrollView.subviews lastObject];
// CGFloat contentH = lastView.frame.origin.y + lastView.frame.size.height;
CGFloat contentH = CGRectGetMaxY(lastView.frame);
NSLog(@"%@", [self.scrollView.subviews lastObject]);
// 水平方向不显示滚动条
self.scrollView.contentSize = CGSizeMake(0, contentH);
NSLog(@"%@", [self.scrollView.subviews lastObject]);
}
- (UIView *)addGridWithX:(CGFloat)x y:(CGFloat)y{
UIView *grid = [[UIView alloc] init];
grid.frame = CGRectMake(x, y, 50, 50);
grid.backgroundColor = [UIColor blueColor];
[self.scrollView addSubview:grid];
return grid;
}
@end
2-显示垂直滚动条隐藏水平滚动条.png
网友评论