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

启动图实现:UIScrollView+UIPageControl简单实现

时间:2016-04-22 16:22:52      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

#import "MJViewController.h"
#import "RootViewController.h"


@interface MJViewController () <UIScrollViewDelegate>
@property (strong, nonatomic) UIScrollView *scrollView;
@property (strong, nonatomic) UIPageControl *pageControl;
@property (strong, nonatomic) UIButton *nextBt;
@end

@implementation MJViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
   
    //初始化视图
    NSArray *array = [NSArray arrayWithObjects:[UIColor redColor],[UIColor blueColor],[UIColor yellowColor],[UIColor purpleColor],[UIColor whiteColor], nil];
    
 
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 80, self.view.frame.size.width-20, self.view.frame.size.height-100)];
    self.scrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.automaticallyAdjustsScrollViewInsets = NO;//恢复scrollview偏移
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES; //分页属性
    self.scrollView.showsHorizontalScrollIndicator= NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.contentSize = CGSizeMake((self.view.frame.size.width-20)*[array count], self.scrollView.frame.size.height); //内容范围
    
    [self.view addSubview:self.scrollView];
    
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(10,self.view.frame.size.height-50, self.view.frame.size.width-20, 20)];
//    self.pageControl.backgroundColor = [UIColor grayColor];
    self.pageControl.numberOfPages = array.count;
    self.pageControl.currentPage = 0;
    [self.view addSubview:self.pageControl];
    
    for (int i = 0; i<array.count; i++) {
        
        UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(i*self.scrollView.frame.size.width+5, 5, self.scrollView.frame.size.width-10, self.scrollView.frame.size.height-10)];
        imageview.backgroundColor = array[i];
        [self.scrollView addSubview:imageview];
        
        if (i==array.count-1) {
            
           imageview.userInteractionEnabled = YES;
       
       self.nextBt = [[UIButton alloc]initWithFrame:CGRectMake(0,0,200, 44)];
        _nextBt.alpha = 0.5;
        _nextBt.backgroundColor = [UIColor darkGrayColor];
        [_nextBt setTitle:@"开启致富之旅" forState:UIControlStateNormal];
        [_nextBt setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        [_nextBt addTarget:self action:@selector(gotoAction) forControlEvents:UIControlEventTouchUpInside];
        [imageview  addSubview:self.nextBt];
        }
    }
}
-(void)gotoAction{
    
    

    RootViewController *rootVC = [[RootViewController alloc]init];
    [self presentModalViewController:rootVC animated:YES];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    if ([scrollView isMemberOfClass:[UITableView class]]) {
        
    }else{
    
        
        //在scrollViewDidScroll内实现监听contentOffset内容偏移量;根据contentOffset计算当前属于哪一页;
        int index = fabs(scrollView.contentOffset.x)/scrollView.frame.size.width;//当前是第几个视图
        self.pageControl.currentPage = index;
    }
}
@end

启动图实现:UIScrollView+UIPageControl简单实现

标签:

原文地址:http://www.cnblogs.com/linxiu-0925/p/5421540.html

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