美文网首页
轮播图

轮播图

作者: BlueLantern | 来源:发表于2016-07-13 10:40 被阅读0次

    ////  ViewController.m//  lunbotu////  Created by lanou on 16/7/13.//  Copyright © 2016年 lanou. All rights reserved.//#import "ViewController.h"@interface ViewController ()#define screenWidth [UIScreen mainScreen].bounds.size.width

    #define screenHeight [UIScreen mainScreen].bounds.size.height

    @property(nonatomic,strong)UIScrollView *scrollView;

    @end

    @implementation ViewController

    - (void)viewDidLoad {

    [super viewDidLoad];

    //创建滑动视图

    self.scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];

    //根据i循环创建UIImageView,再添加到滑动视图scrollView上面

    for (NSInteger i= 0; i<6;i++){

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i*screenWidth,0,screenWidth,screenHeight)];

    NSString *imageName = nil;

    if(i==5){

    imageName = @"1.jpg";}else{imageName = [NSString stringWithFormat:@"%ld.jpg",i+1 ];}

    //加载响应图片

    UIImage *image = [UIImage imageNamed:imageName];

    //设置图片

    imageView.image = image;

    //将Imageview添加到视图上

    [self.scrollView addSubview:imageView];

    }

    //添加滑动视图到屏幕上

    [self.view addSubview:self.scrollView];

    //设置滑动视图的滑动区域

    self.scrollView.contentSize = CGSizeMake(6*screenWidth,screenHeight);

    //整屏翻转

    self.scrollView.pagingEnabled = YES;

    //边界回弹

    self.scrollView.bounces = NO;

    //偏移量

    self.scrollView.delegate = self;//设置代理,代理是负责监听滑动视图整个滑动的过程

    }

    //scrollview在滑动视图滑动的时候调用,

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

    //contentOffset  是访问到了滑动视图偏移量,包含了x和y的偏移量  offset是偏移量。

    //setContentOffset:anmimate:

    NSLog(@"offset.x = %f,offset.y = %f",scrollView.contentOffset.x,scrollView.contentOffset.y);

    }

    - (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    }

    @end

    相关文章

      网友评论

          本文标题: 轮播图

          本文链接:https://www.haomeiwen.com/subject/nivsjttx.html