iOS UI06_UIScorollView

//
//  MainViewController.m
//  UI06_UIScorollView
//
//  Created by dllo on 15/8/5.
//  Copyright (c) 2015年 zhozhicheng. All rights reserved.
//

#import "MainViewController.h"
//定义两个宏,分别为屏幕尺寸的宽,高
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface MainViewController ()<UIScrollViewDelegate>
@property(nonatomic,retain)UIScrollView *scrollView;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor yellowColor];
    //创建一个scrollView,和View尺寸一样大
    UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:self.view.frame];
    scrollView.backgroundColor=[UIColor redColor];
    [self.view addSubview:scrollView];
    [scrollView release];
    //重要的属性,这个属性可以让ScrollView滚动起来
    //contenSize设置scrollView的滚动范围
    scrollView.contentSize =CGSizeMake(15 * WIDTH, HEIGHT);
    //按页来进行滚动
    scrollView.pagingEnabled=YES;
    //
    for (NSInteger i=0; i < 13; i++) {
        //最常用的拼接
        NSString *picName =[NSString stringWithFormat:@"cymbal_%02ld.jpg",i];
        //通过图片名来创建UIImageView
        UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:picName]];
        //设置每一张图片的起始位置
        imageView.frame=CGRectMake(((i+1) * WIDTH),0, WIDTH, HEIGHT);
        //把imageView图片放到scrollView
        [scrollView addSubview:imageView];
        [imageView release];
    }
    //额外添加两张图,一张放在第一个,一个放在最后一个,对应的上面的整体范围+2
    UIImageView *last=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cymbal_00.jpg"]];
    last.frame=CGRectMake(14 * WIDTH, 0, WIDTH, HEIGHT);
    [scrollView addSubview:last];
    [last release];

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

    //偏移量(把第几张放到最前面)
    scrollView.contentOffset=CGPointMake(WIDTH , 0);
    //关掉边界回弹效果,默认是YES
    scrollView.bounces=NO;
    //关闭滚动条,竖直,横的  水平和竖直两个滚动条会作为两个子视图添加到scrollView的子视图中,如果把滚动条效果关闭,这两个视图就不会添加到子视图中
    scrollView.showsHorizontalScrollIndicator=NO;
    scrollView.showsVerticalScrollIndicator=NO;
    //设置代理人
    scrollView.delegate=self;
    scrollView.tag=1000;

    //创建一个计时器
    [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
}
#pragma mark 只要滚动就会触发的协议方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (CGPointEqualToPoint(scrollView.contentOffset, CGPointMake(WIDTH * 14, 0))) {
        scrollView.contentOffset=CGPointMake(WIDTH, 0);
    }
    if (CGPointEqualToPoint(scrollView.contentOffset, CGPointMake(0, 0))) {
        scrollView.contentOffset=CGPointMake(WIDTH * 13, 0);
    }

}
#pragma mark 当scrollView减速时触发停止
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"滚动停止");

}
-(void)changeImage
{
    //通过tag找scrollView
    UIScrollView *scrollView=(UIScrollView *)[self.view viewWithTag:1000];
//    scrollView.contentOffset=CGPointMake(scrollView.contentOffset.x + WIDTH, 0);
    [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x + WIDTH, 0) animated:YES];
    if (scrollView.contentOffset.x ==WIDTH * 14) {
        scrollView.contentOffset =CGPointMake(WIDTH, 0);
    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值