码迷,mamicode.com
首页 > 其他好文 > 详细

Masonry之UIScrollView 自动布局

时间:2016-01-08 10:26:35      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

  由于项目开始比较急,又是一个人,再加上apple自动布局比较麻烦,衡量以后就用了frame来布局画面。现在稍微闲了一些,就开始对之前的代码做一些优化。其中有一个小功能是这样的,一个可以横向滑动的scrollView,画面加载的时候从服务器取背景图以及文字说明和跳转链接,同时图片做缓存,下一次重新加载的时候如果加载失败,就加载上一次的内容。那么这个地方就涉及了画面,图片缓存两个部分。本文先从画面开始说起。

  第一次使用自动布局来写scrollView,就遇到了坑。后来在一个博客中找到了答案。链接如下:UIScrollView添加AutoLayout约束的坑,核心的意思是:

  1、scrollView内部子控件的尺寸不能以scrollView的尺寸为参照
  2、scrollView内部的子控件的约束必须完整

  设计思路,子视图(AdvertisementView)一个view,滚动视图(AdvertisementScrollView)一个view,然后滚动视图中添加scrollView,ViewController里边直接添加滚动视图。

  代码如下:

  AdvertisementView.h

#import <UIKit/UIKit.h>

@interface AdvertisementView : UIView

@property (nonatomic, strong) UILabel *textLabel;

@property (nonatomic, strong) UILabel *textDetailLabel;

- (void)setTextColor:(UIColor *)textColor text:(NSString *)text textDetail:(NSString *)textDetail;

@end

 

  AdvertisementView.m

#import "AdvertisementView.h"
#import "Define.h"

@implementation AdvertisementView

- (id)init {
    self = [super init];
    if (self) {
        self.textLabel = [[UILabel alloc] init];
        [self.textLabel setFont:[UIFont fontWithName:FONT_NAME size:15]];
        [self.textLabel setTextAlignment:NSTextAlignmentRight];
        [self addSubview:self.textLabel];
        
        self.textDetailLabel = [[UILabel alloc] init];
        [self.textDetailLabel setFont:[UIFont fontWithName:FONT_NAME size:25]];
        [self.textDetailLabel setTextAlignment:NSTextAlignmentRight];
        [self addSubview:self.textDetailLabel];
    }
    
    return self;
}

- (void)layoutSubviews {
    
    [self.textDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(0);
        make.right.equalTo(self).with.offset(-15);
        make.bottom.equalTo(self).with.offset(-15);
        make.height.mas_equalTo(@25);
    }];
    
    [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(0);
        make.right.equalTo(self).with.offset(-15);
        make.bottom.equalTo(self.textDetailLabel.mas_top).with.offset(-5);
        make.height.mas_equalTo(@15);
    }];
}

#pragma mark - load data

- (void)setTextColor:(UIColor *)textColor text:(NSString *)text textDetail:(NSString *)textDetail {
    if (self) {
        [self.textLabel setText:text];
        [self.textLabel setTextColor:textColor];
        
        [self.textDetailLabel setText:textDetail];
        [self.textDetailLabel setTextColor:textColor];
    } else {
        NSLog(@"Error : AdvertisementView class is not init successfully.");
    }
}

@end

  AdvertisementScrollView.h

#import <UIKit/UIKit.h>
#import "AdvertisementView.h"

@interface AdvertisementScrollView : UIView <UIScrollViewDelegate> {
    NSArray *_viewArray;
}

@property (nonatomic, strong) NSArray *viewArray;

@end

  AdvertisementScrollView.m

#import "AdvertisementScrollView.h"

@interface AdvertisementScrollView ()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIPageControl *pageControl;

@end

@implementation AdvertisementScrollView

- (id)init {
    self = [super init];
    
    if (self) {
        [self addSubview:self.scrollView];
    }
    
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.mas_bottom);
        make.centerX.equalTo(self.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(self.bounds.size.width/2 , 30));
    }];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.height.equalTo(self.scrollView);
    }];
    
    AdvertisementView *lastAdView = nil;
    
    for (AdvertisementView *adView in self.viewArray) {
        [adView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(lastAdView ? lastAdView.mas_right : @0);
            make.bottom.equalTo(@0);
            make.width.equalTo(self.scrollView.mas_width);
            make.height.equalTo(self.scrollView.mas_height);
        }];
        
        lastAdView = adView;
    }
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lastAdView.mas_right);
    }];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat pageWidth = scrollView.frame.size.width;
    int currentPage = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    
    self.pageControl.currentPage = currentPage;
}

#pragma mark - getters&setters

- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = [UIColor redColor];
        _scrollView.delegate = self;
        _scrollView.bounces = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        
        [_scrollView addSubview:self.contentView];
    }
    
    return _scrollView;
}

- (UIView *)contentView {
    if (_contentView == nil) {
        _contentView = [[UIView alloc] init];
    }
    
    return _contentView;
}

- (UIPageControl *)pageControl {
    if (_pageControl == nil) {
        _pageControl = [[UIPageControl alloc] init];
        [_pageControl setCurrentPageIndicatorTintColor:[UIColor whiteColor]];
        [_pageControl setPageIndicatorTintColor:[UIColor grayColor]];
        [_pageControl setCurrentPage:0];
        [_pageControl setNumberOfPages:[self.viewArray count]];
    }
    
    return _pageControl;
}

- (NSArray *)viewArray {
    return _viewArray;
}

- (void)setViewArray:(NSArray *)viewArray {
    _viewArray = viewArray;
    
    for (AdvertisementView *adView in self.viewArray) {
        [self.contentView addSubview:adView];
    }
    [self addSubview:self.pageControl];
}

@end

  MainPersonalInfoViewController.h

#import <UIKit/UIKit.h>
#import "AdvertisementScrollView.h"
#import "AdvertisementView.h"

@interface MainPersonalInfoViewController : UIViewController {
    AdvertisementScrollView *_adScrollView;
}

@property (nonatomic, strong) AdvertisementScrollView *adScrollView;

@end

  MainPersonalInfoViewController.m

#import "MainPersonalInfoViewController.h"

@interface MainPersonalInfoViewController ()

@end

@implementation MainPersonalInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.adScrollView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.adScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(0);
        make.right.equalTo(self.view).with.offset(0);
        make.top.equalTo(self.view).with.offset(0);
        make.height.mas_equalTo(@150);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - getters&setters

- (AdvertisementScrollView *)adScrollView {
    if (_adScrollView == nil) {
        _adScrollView = [[AdvertisementScrollView alloc] init];
        [_adScrollView setBackgroundColor:[UIColor grayColor]];
        
        AdvertisementView *adView1 = [[AdvertisementView alloc] init];
        [adView1 setBackgroundColor:[self randomColor]];
        [adView1 setTextColor:[self randomColor] text:@"2.0T四缸又如何?" textDetail:@"测试奥迪Q7 40TFSI S line"];
        
        AdvertisementView *adView2 = [[AdvertisementView alloc] init];
        [adView2 setBackgroundColor:[self randomColor]];
        [adView2 setTextColor:[self randomColor] text:@"解读低配车" textDetail:@"实拍日产逍客1.2T车型"];
        
        [_adScrollView setViewArray:[@[adView1, adView2] mutableCopy]];
    }
    
    return _adScrollView;
}

- (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];
}

@end

  以上。

 

Masonry之UIScrollView 自动布局

标签:

原文地址:http://www.cnblogs.com/zpz501/p/5112226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!