pureLayout简单使用ScrollView

作者: 酒红色T恤 | 来源:发表于2016-07-12 23:38 被阅读160次

//
// ViewController.m
// TestPureLayout
//
// Created by 哲理 on 16/7/12.
// Copyright © 2016年 Joseph. All rights reserved .

  #import "ViewController.h"
  #import "ALView+PureLayout.h"

 @interface ViewController ()<UIScrollViewDelegate>
 /** view */
 @property (nonatomic,strong) UIImageView *myImageView;

 /** button */
 @property (nonatomic,strong) UIButton *button;

 /** scrollView */
 @property (nonatomic,strong) UIScrollView *myScrollView;
 @end

 @implementation ViewController
 #define Screen_Width     [UIScreen mainScreen].bounds.size.width
 #define Screen_Height     [UIScreen mainScreen].bounds.size.height

 - (void)viewDidLoad {

[super viewDidLoad];
_myImageView = [ UIImageView newAutoLayoutView];
_myImageView.backgroundColor = [ UIColor redColor];
[self.view addSubview:_myImageView];

[ _myImageView autoPinEdgeToSuperviewEdge: ALEdgeTop];
[ _myImageView autoPinEdgeToSuperviewEdge: ALEdgeLeading];

[_myImageView autoSetDimension:ALDimensionWidth toSize:Screen_Width];
[_myImageView autoSetDimension: ALDimensionHeight toSize:100];

_myScrollView = [ UIScrollView newAutoLayoutView];
_myScrollView.delegate =self;
_myScrollView.bounces = NO;
_myScrollView.contentSize = CGSizeMake(Screen_Width, 2*(Screen_Height - 100));


_myScrollView.backgroundColor = [ UIColor blueColor];
_myScrollView.scrollEnabled = NO;
[self.view addSubview:_myScrollView];
[_myScrollView autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.myImageView];
[ _myScrollView autoPinEdgeToSuperviewEdge: ALEdgeLeading];

[_myScrollView autoSetDimension:ALDimensionWidth toSize:Screen_Width];
[_myScrollView autoSetDimension: ALDimensionHeight toSize:(Screen_Height - 100)];

for (int i = 0; i < 2; i++) {
    UILabel * label = [ UILabel newAutoLayoutView];
    label.backgroundColor = [ UIColor greenColor];
    [_myScrollView addSubview:label];
    
    label.text = [NSString stringWithFormat:@"第%d个",i];
    [label autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:_myScrollView withOffset:(i*(Screen_Height - 100))];
    [label autoSetDimension:ALDimensionWidth toSize:Screen_Width];
    [label autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.myScrollView withOffset:0];
    [label autoMatchDimension:ALDimensionHeight toDimension:ALDimensionHeight ofView:_myScrollView];
    if (i == 0) {
        UIButton * button1 = [ UIButton buttonWithType:UIButtonTypeCustom];
        button1.backgroundColor = [ UIColor yellowColor];
        
        [self.myScrollView addSubview:button1];
 //            [self.view bringSubviewToFront:button1];
        [button1 setTitle:@"立即失算" forState:UIControlStateNormal];
        [button1 addTarget:self action:@selector(clickChangeContentoffset) forControlEvents:UIControlEventTouchUpInside];
         [button1 autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:_myScrollView withOffset:0];
        [button1 autoSetDimension:ALDimensionHeight toSize:40];
         [button1 autoSetDimension:ALDimensionWidth toSize:(Screen_Width-2*20)];
        [button1 autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:_myScrollView withOffset:20];
        
    }
}
}

 -(void)clickChangeContentoffset{
NSLog(@"fas");
[UIView animateWithDuration:1.2 animations:^{
 self.myScrollView.contentOffset = CGPointMake(0, Screen_Height - 100);
}];

 }

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
NSLog(@"%f",scrollView.contentOffset.y);
if(scrollView.contentOffset.y ==0){
    self.myScrollView.scrollEnabled = NO;
}else{
    self.myScrollView.scrollEnabled = YES;
}
 }

 @end

相关文章

网友评论

    本文标题:pureLayout简单使用ScrollView

    本文链接:https://www.haomeiwen.com/subject/bafydttx.html