AutoAdaptationScrollView能夠適應自動內容大小的ScrollView

分享個能夠自動適應大小的ScrollView 


下面貼下適應的代碼

ViewController.m

#import "ViewController.h"
#import "AutoAdaptationScrollView.h"


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    
    AutoAdaptationScrollView *autoSizeView = [[AutoAdaptationScrollView alloc] init];
    // 設置ScrollView的方向
    autoSizeView.directionType = AutoAdaptationScrollViewPortrait;
    
    autoSizeView.frame = CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 50);
    autoSizeView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:autoSizeView];
    
    
    // 內容的View
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(autoSizeView.frame), 900)];
    [autoSizeView addSubview:contentView];
    
    
    //子視圖1
    UIView *AView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(autoSizeView.frame), 370)];
    AView.backgroundColor = [UIColor blueColor];
    [contentView addSubview:AView];

    //子視圖1
    UIView *BView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(AView.frame), CGRectGetWidth(autoSizeView.frame), 370)];
    BView.backgroundColor = [UIColor greenColor];
    [contentView addSubview:BView];
    //讓ScrollView的大小適應內容視圖
    [autoSizeView autoAdaptationWithView:contentView];
    
    
}

AutoAdaptationScrollView.h

#import <UIKit/UIKit.h>


typedef enum
{
    AutoAdaptationScrollViewPortrait,
    AutoAdaptationScrollViewLandscape
}AutoAdaptationScrollViewDirection;



@interface AutoAdaptationScrollView : UIScrollView

@property (nonatomic , assign) AutoAdaptationScrollViewDirection directionType;

- (void)autoAdaptationWithView:(UIView *)view;


@end
AutoAdaptationScrollView.m
#import "AutoAdaptationScrollView.h"

@implementation AutoAdaptationScrollView


- (id) init
{
    if (self = [super init]) {
        _directionType = AutoAdaptationScrollViewPortrait;
    }
    return self;
}

- (id) initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _directionType = AutoAdaptationScrollViewPortrait;
    }
    return self;
}

- (void)autoAdaptationWithView:(UIView *)view
{
    
    if (_directionType == AutoAdaptationScrollViewPortrait) {
        NSArray *subviews = view.subviews;
        __block UIView *lastView;
        [UIView animateWithDuration:0.5f animations:^{
            for (UIView *view in subviews) {
                [self setViewY:CGRectGetMaxY(lastView.frame) view:view];
                lastView = view;
            }
            [self setContentSize:CGSizeMake(0.0f, CGRectGetMaxY(lastView.frame))];
            
        }];
    }else if (_directionType == AutoAdaptationScrollViewLandscape)
    {
        NSArray *subviews = view.subviews;
        __block UIView *lastView;
        [UIView animateWithDuration:0.5f animations:^{
            for (UIView *view in subviews) {
                [self setViewX:CGRectGetMaxX(lastView.frame) view:view];
                
                lastView = view;
            }
            [self setContentSize:CGSizeMake(CGRectGetMaxX(lastView.frame), 0.0f)];
            
        }];
    }
 
}

// 設置UIView的Y
- (void)setViewY:(CGFloat)newY view:(UIView *)mView
{
    CGRect viewFrame = [mView frame];
    viewFrame.origin.y = newY;
    [mView setFrame:viewFrame];
}

// 設置UIView的X
- (void)setViewX:(CGFloat)newX view:(UIView *)mView
{
    CGRect viewFrame = [mView frame];
    viewFrame.origin.x = newX;
    [mView setFrame:viewFrame];
    
}

@end


代碼下載 http://download.csdn.net/detail/qqmcy/8460365

發佈了488 篇原創文章 · 獲贊 22 · 訪問量 115萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章