UIScrollview的使用方法:

1.今天学习了一下UIScrollveiw的使用方法:就是在欢迎界面的使用来介绍产品的新特性的代码:

下面有两种方法来实现其中代码:(不多说废话,直接来代码)使用xib实现的

#import "WecomeViewController1.h"

#import "AppDelegate.h"

#import "QuestionViewController.h"

@interface WecomeViewController1 ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;

@property (weak, nonatomic) IBOutlet UIPageControl *pagecount;

 @end

 @implementation WecomeViewController1

- (void)viewDidLoad {

    [super viewDidLoad];

    [self setUpscrollview];

    [self setUppageview];

   }

 -(void)setUpscrollview{

        self.scrollview.delegate = self; //设置代理

    //设置图片

    UIImageView *imageview1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cover1.png"]];

    UIImageView *imageview2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cover2.png"]];

    UIImageView *imageview3 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cover3.png"]];

    //设置frmae

    imageview1.frame = CGRectMake(0, 0, screenWitdth, screenHeight);

    imageview2.frame = CGRectMake(screenWitdth, 0, screenWitdth, screenHeight);

    imageview3.frame = CGRectMake(2*screenWitdth, 0, screenWitdth, screenHeight);

    //填充模式

    imageview1.contentMode = UIViewContentModeScaleAspectFit;

    imageview2.contentMode = UIViewContentModeScaleAspectFit;

    imageview3.contentMode = UIViewContentModeScaleAspectFit;

    //在第三张图片上面加一个button

     imageview3.userInteractionEnabled = YES;

    UIButton *customButton = [[UIButton alloc]init];

    [customButton setBackgroundImage:[UIImage imageNamed:@"start1.png"] forState:UIControlStateNormal];

    CGFloat centerX = imageview3.frame.size.width * 0.5;

    CGFloat centerY = imageview3.frame.size.height * 0.9;

    customButton.center = CGPointMake(centerX, centerY);

    customButton.bounds = (CGRect){CGPointZero, 100 ,40};

    [customButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];

    [imageview3 addSubview:customButton];

    //将图添加到scrllview上面去

    [self.scrollview addSubview:imageview1];

    [self.scrollview addSubview:imageview2];

    [self.scrollview addSubview:imageview3];

    //设置scrollvew的一些属性

    self.scrollview.contentSize = CGSizeMake(3*screenWitdth, 0);

    self.scrollview.pagingEnabled = YES;

    self.scrollview.showsHorizontalScrollIndicator = NO;

    self.scrollview.showsVerticalScrollIndicator = NO;

    self.scrollview.bounces = NO;

 }

 -(void)login{

    NSLog(@"登陆界面");

    QuestionViewController *questVC = [[QuestionViewController alloc]init];

    [self presentViewController:questVC animated:YES completion:nil];

}

 //创建pagecontoller

-(void)setUppageview{

      self.pagecount.numberOfPages = 3;

    self.pagecount.currentPageIndicatorTintColor = [UIColor grayColor];

    self.pagecount.pageIndicatorTintColor = [UIColor blackColor];

 }

 #pragma mark -UIScrollDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

   //计算水平的距离

    CGFloat offsetX = self.scrollview.contentOffset.x;

    //求页码

    double pageDouble = offsetX / scrollView.frame.size.width;

    int pageInt = (int)(pageDouble + 0.5);

    self.pagecount.currentPage = pageInt;  

}

@end

 

第二种纯代码实现:

#define CWNewfeatureImageCount 3

#import "CWNewfeatureViewController.h"

#import "CWTabBarViewController.h"

 @interface CWNewfeatureViewController ()<UIScrollViewDelegate>

 @property (nonatomic, weak) UIPageControl *pageControl;

 @end

 @implementation CWNewfeatureViewController

 - (void)viewDidLoad

{

    [super viewDidLoad];

    

    // 1.添加UISrollView

    [self setupScrollView];

    

    // 2.添加pageControl

    [self setupPageControl];

}

/**

 *  添加pageControl

 */

- (void)setupPageControl

{

    // 1.添加

    UIPageControl *pageControl = [[UIPageControl alloc] init];

    pageControl.numberOfPages = CWNewfeatureImageCount;

    CGFloat centerX = self.view.frame.size.width * 0.5;

    CGFloat centerY = self.view.frame.size.height - 30;

    pageControl.center = CGPointMake(centerX, centerY);

    pageControl.bounds = CGRectMake(0, 0, 100, 30);

    pageControl.userInteractionEnabled = NO;

    [self.view addSubview:pageControl];

    self.pageControl = pageControl;

    // 2.设置圆点的颜色

    pageControl.currentPageIndicatorTintColor = CWColor(253, 98, 42);

    pageControl.pageIndicatorTintColor = CWColor(189, 189, 189);

}

-(void)setupScrollView

{

    UIScrollView *scrollView = [[UIScrollView alloc] init];

    scrollView.frame = self.view.bounds;

    scrollView.delegate = self;

    [self.view addSubview:scrollView];

    

    // 2.添加图片

    CGFloat imageW = scrollView.frame.size.width;

    CGFloat imageH = scrollView.frame.size.height;

    for (int index = 0; index<CWNewfeatureImageCount; index++) {

        UIImageView *imageView = [[UIImageView alloc] init];

         // 设置图片

        NSString *name = nil;

        if (fourInch) {

            name = [NSString stringWithFormat:@"new_feature_%d-568h", index + 1];

        } else {

            name = [NSString stringWithFormat:@"new_feature_%d", index + 1];

        }

        imageView.image = [UIImage imageWithName:name];

         // 设置frame

        CGFloat imageX = index * imageW;

        imageView.frame = CGRectMake(imageX, 0, imageW, imageH);

        

        [scrollView addSubview:imageView];

        // 在最后一个图片上面添加按钮

        if (index == CWNewfeatureImageCount - 1) {

            [self setupLastImageView:imageView];

        }

    }

    

    // 3.设置滚动的内容尺寸

    scrollView.contentSize = CGSizeMake(imageW * CWNewfeatureImageCount, 0);

    scrollView.showsHorizontalScrollIndicator = NO;

    scrollView.pagingEnabled = YES;

    scrollView.bounces = NO;

    

}

/**

 *  添加内容到最后一个图片

 */

- (void)setupLastImageView:(UIImageView *)imageView

{

    // 0.让imageView能跟用户交互

    imageView.userInteractionEnabled = YES;

    

    // 1.添加开始按钮

    UIButton *startButton = [[UIButton alloc] init];

    [startButton setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button"] forState:UIControlStateNormal];

    [startButton setBackgroundImage:[UIImage imageWithName:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];

    

    // 2.设置frame

    CGFloat centerX = imageView.frame.size.width * 0.5;

    CGFloat centerY = imageView.frame.size.height * 0.6;

    startButton.center = CGPointMake(centerX, centerY);

    startButton.bounds = (CGRect){CGPointZero, startButton.currentBackgroundImage.size};

    

    // 3.设置文字

    [startButton setTitle:@"开始微博" forState:UIControlStateNormal];

    [startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [startButton addTarget:self action:@selector(start) forControlEvents:UIControlEventTouchUpInside];

    [imageView addSubview:startButton];

    

    // 4.添加checkbox

    UIButton *checkbox = [[UIButton alloc] init];

    checkbox.selected = YES;

    [checkbox setTitle:@"分享给大家" forState:UIControlStateNormal];

    [checkbox setImage:[UIImage imageWithName:@"new_feature_share_false"] forState:UIControlStateNormal];

    [checkbox setImage:[UIImage imageWithName:@"new_feature_share_true"] forState:UIControlStateSelected];

    checkbox.bounds = CGRectMake(0, 0, 200, 50);

    CGFloat checkboxCenterX = centerX;

    CGFloat checkboxCenterY = imageView.frame.size.height * 0.5;

    checkbox.center = CGPointMake(checkboxCenterX, checkboxCenterY);

    [checkbox setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    checkbox.titleLabel.font = [UIFont systemFontOfSize:15];

    [checkbox addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside];

    

    checkbox.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);

    [imageView addSubview:checkbox];

    

}

 - (void)checkboxClick:(UIButton *)checkbox

{

    checkbox.selected = !checkbox.isSelected;

}

 /**

 *  开始微博

 */

- (void)start

{

    // 显示状态栏

    [UIApplication sharedApplication].statusBarHidden = NO;

    // 切换窗口的根控制器

    self.view.window.rootViewController = [[CWTabBarViewController alloc] init];

}

 /**

 *  只要UIScrollView滚动了,就会调用

 *

 */

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    // 1.取出水平方向上滚动的距离

    CGFloat offsetX = scrollView.contentOffset.x;

    

    // 2.求出页码

    double pageDouble = offsetX / scrollView.frame.size.width;

    int pageInt = (int)(pageDouble + 0.5);

    self.pageControl.currentPage = pageInt;

}

 @end

 效果图:

 

posted @ 2015-07-11 09:43  朱峰博客  阅读(197)  评论(0编辑  收藏  举报