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

block对象传递事件

时间:2014-08-21 14:58:54      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   strong   for   ar   div   

MyScrollView.h

@property (nonatomic,assign) NSInteger currentPage;

@property (nonatomic,strong) NSInteger (^numberOfPage)(MyScrollView *);
@property (nonatomic,strong) UIView *(^scroll)(MyScrollView *,NSInteger);

- (void)loadScroll;

 

MyScrollView.m

@interface MyScrollView ()
{
    UIScrollView *_scrollView;
}
@end
@implementation MyScrollView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {       
        _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _scrollView.pagingEnabled = YES;
        
        [self addSubview:_scrollView];
    }
    return self;
}

- (void)loadScroll
{
    [_scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];   //removeFromSuperview:从父视图中移除
    
    NSInteger count = _numberOfPage(self);
    
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    _scrollView.contentSize = CGSizeMake(width * count, height);
    
    for (NSInteger i = 0; i < count; i++) {        
        UIView *viewer = _scroll(self,i);      
        viewer.frame = CGRectMake(i * width, 0, width, height);
        [_scrollView addSubview:viewer];        
    }
}
- (NSInteger)currentPage
{
    return _scrollView.contentOffset.x / _scrollView.contentSize.width;
}
- (void)setCurrentPage:(NSInteger)currentPage
{
    _scrollView.contentOffset = CGPointMake(_scrollView.frame.size.width, 0);
    currentPage = currentPage;
}
@end

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    
    MyScrollView *myScroll = [[MyScrollView alloc] initWithFrame:CGRectMake(0, 50, 320, 160)];  
    myScroll.numberOfPage = ^(MyScrollView *scrollView){
        return 10;
    };
    
    myScroll.scroll = ^(MyScrollView *scrollView,NSInteger index){    
        double value = arc4random() % 256;
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor colorWithRed:0.1 green:0.4 blue:value/256 alpha:1];        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
        label.text = [NSString stringWithFormat:@"page: %d",index];
        [view addSubview:label];        
        return view;
    };
    [self.view addSubview:myScroll];   
    [myScroll loadScroll];
    myScroll.currentPage = 0;    
}

 

block对象传递事件,布布扣,bubuko.com

block对象传递事件

标签:style   blog   color   io   strong   for   ar   div   

原文地址:http://www.cnblogs.com/annboo/p/3927083.html

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