Skip to content

Instantly share code, notes, and snippets.

@wongzigii
Forked from 0oneo/UIScrollView with masonry
Created October 12, 2017 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wongzigii/ebb5d2be387f66d7f982110cf09b69f9 to your computer and use it in GitHub Desktop.
Save wongzigii/ebb5d2be387f66d7f982110cf09b69f9 to your computer and use it in GitHub Desktop.
vertical and Horizontal scrollview using masonry
@interface ViewController ()
@property (strong, nonatomic) UIScrollView* vScrollView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *verticalScrollView = UIScrollView.new;
self.vScrollView = verticalScrollView;
verticalScrollView.backgroundColor = [UIColor grayColor];
[self.view addSubview:verticalScrollView];
[self.vScrollView makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
// We create a dummy contentView that will hold everything (necessary to use scrollRectToVisible later)
UIView* contentView = UIView.new;
[self.vScrollView addSubview:contentView];
[contentView makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.vScrollView); // let the uiscrollview know where the edges of the contentView
make.width.equalTo(self.view.width); // contentView's must can be calculated based on the constraits you set \
and let the autolayout calculate the width of the contentView;
}];
UIView *lastView;
CGFloat height = 65;
for (int i = 1; i <= 10; i++) {
UIView *view = UIView.new;
//view.backgroundColor = [self randomColor];
view.tag = i;
[contentView addSubview:view];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
[view addGestureRecognizer:singleTap];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(lastView ? lastView.bottom : @0);
make.left.equalTo(contentView.left);
make.width.equalTo(contentView.width);
make.height.equalTo(@(height));
}];
//add Subview + content
[self createHorizontalScrollView:view];
lastView = view;
}
[contentView makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(lastView.bottom); // contentView's must can be calculated based on the constraits you set \
and let the autolayout calculate the height of the contentView;
}];
}
- (void)singleTap:(UITapGestureRecognizer*)sender {
//[sender.view setAlpha:sender.view.alpha / 1.20]; // To see something happen on screen when you tap :O
[self.vScrollView scrollRectToVisible:sender.view.frame animated:YES];
NSLog(@"touched row %d",sender.view.tag);
};
- (id)createHorizontalScrollView:(UIView*)rowView
{
UIScrollView *scrollView = UIScrollView.new;
scrollView.backgroundColor = [UIColor grayColor];
[rowView addSubview:scrollView];
[scrollView makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(rowView);
}];
// We create a dummy contentView that will hold everything (necessary to use scrollRectToVisible later)
UIView* contentView = UIView.new;
[scrollView addSubview:contentView];
[contentView makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(scrollView);
make.height.equalTo(rowView.height);
}];
UIView *lastElementView;
CGFloat width = 65;
for (int i = 1; i <= 10; i++) {
UIView *elementView = UIView.new;
elementView.backgroundColor = [self randomColor];
elementView.tag = i + (rowView.tag * 10);
[contentView addSubview:elementView];
// Tap
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(elementTap:)];
[elementView addGestureRecognizer:singleTap];
[elementView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(lastElementView ? lastElementView.right : @0);
make.top.equalTo(contentView.top);
make.height.equalTo(contentView.height);
make.width.equalTo(@(width));
}];
lastElementView = elementView;
}
[contentView makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(lastElementView.right);
}];
return nil;
}
- (UIColor *)randomColor {
CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}
- (void)elementTap:(UITapGestureRecognizer*)sender {
[sender.view setAlpha:sender.view.alpha / 1.20]; // To see something happen on screen when you tap :O
[self.vScrollView scrollRectToVisible:sender.view.frame animated:YES];
NSLog(@"touched element %d",sender.view.tag);
};
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment