ScrollView

#import "MainViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface MainViewController ()<UIScrollViewDelegate>

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor=[UIColor whiteColor];

    UIScrollView *scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    scrollView.backgroundColor=[UIColor yellowColor];
    [self.view addSubview:scrollView];

    [scrollView release];


    // 重要的属性,这个属性可以让scrollView滚动起来
    // contentSize设置scrollView的滚动范围
    scrollView.contentSize=CGSizeMake(WIDTH * 6, HEIGHT);

    // 按页来进行滚动
    scrollView.pagingEnabled=YES;
    UIImageView  *imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"4.jpg"]];
    imageView.frame=CGRectMake(0,0,WIDTH, HEIGHT);
    [scrollView addSubview:imageView];
    [imageView release];
    for (NSInteger i=1; i<5; i++) {
        // 拼接图片名
        NSString *picName=[NSString stringWithFormat:@"%ld.jpg",i];
        // 通过图片名来创建UIImageView
        UIImageView *imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:picName]];
        imageView.frame=CGRectMake(WIDTH *i,0,WIDTH, HEIGHT);

        // 把imageView放到scrollView中
        [scrollView addSubview:imageView];
        [imageView release];
//         NSMutableArray *PicArr=[NSMutableArray array];

    }

    UIImageView *imageView2=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.jpg"]];
    imageView2.frame=CGRectMake(WIDTH *5,0,WIDTH, HEIGHT);
    [scrollView addSubview:imageView2];
    [imageView2 release];




   // 偏移量
    scrollView.contentOffset=CGPointMake(WIDTH * 2, 0);

    // 关掉默认的边界回弹效果
    scrollView.bounces=NO;

    NSLog(@"%@",scrollView.subviews);

    // 水平和垂直的滚动条会作为两个子视图添加到scrollView的子视图里,如果把滚动条效果关闭,这两个视图就不会添加到scrollView的子视图里

    // 关闭滚动条
    scrollView.showsHorizontalScrollIndicator=NO;
    scrollView.showsVerticalScrollIndicator=NO;

    // 代理
    // 滚动  缩放
    scrollView.delegate=self;
    // 设置tag值
    scrollView.tag=1;

    // 创建一个计时器
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changeImage) userInfo:nil repeats:YES]    
}
-(void)changeImage
{
    // 通过tag值找scrollView
    UIScrollView *scrollView=(UIScrollView *)[self.view viewWithTag:1];

    [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x+WIDTH, 0) animated:YES];
    if (scrollView.contentOffset.x==WIDTH * 6) {
        scrollView.contentOffset=CGPointMake(WIDTH, 0);
    }
}       


#pragma mark 滚动就会触发
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x==0) {
        scrollView.contentOffset=CGPointMake(WIDTH * 4, 0);
    }
    else if (scrollView.contentOffset.x==(WIDTH * 5))
    {
        scrollView.contentOffset=CGPointMake(WIDTH * 1, 0);
    }
}

#pragma mark 当scrollView减速停止时会触发
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"减速时停止");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值