UI_滚动视图自动翻页应用

#import "AppDelegate.h"

#import "ViewController.h"


@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];


    ViewController *viewVC = [[ViewController alloc]init];

    self.window.rootViewController =viewVC;

    

    

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    return YES;


@end






#import "ViewController.h"

#import "CellView.h"

#import "UIscrollView.h"

@interface ViewController ()<UIScrollViewDelegate>


@property(nonatomic,retain)NSTimer *Timer;    //让视图自动播放

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

 

    //初始化

    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];

    //滚动范围

    scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame)*3,CGRectGetHeight(self.view.frame));

    //隐藏滚动条

    scrollView.showsVerticalScrollIndicator = NO;

    [self.view addSubview:scrollView];

    

    //创建三个页面加三张图

    

    NSMutableArray *array = [[NSMutableArray alloc]initWithObjects:@"One.png",@"Two.png",@"Three.png", nil];

    for (int i =0 ; i<3; i++) {

        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*i, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];

        

        [imageView setImage:[UIImage imageNamed:array[i]]];

        [scrollView addSubview:imageView];

    }

    

    //设置tag

    scrollView.tag = 1000;

    //设置滚动视图代理

    scrollView.delegate = self;

    

    //启动定时器

    self.Timer =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];

    

    

    

}


-(void)timeAction{

    //得到滚动视图

    UIScrollView *scrollView =(UIScrollView *)[self.view viewWithTag:1000];

    //通过偏移量来切换滚动视图子页面

    float offset = scrollView.contentOffset.x;

    //每次切换一个屏幕界面

    offset +=CGRectGetWidth(self.view.frame);

    

    

    

    //判断是否是最后一个子视图

    if (offset > 2*CGRectGetWidth(self.view.frame)) {

        offset = 0;

    }

    

    //得到最终的偏移量

    CGPoint resultPoint = CGPointMake(offset, 0);

    //切换视图的时候带动画效果

    //从最后一张回到第一张的时候,不带动画效果

    if (offset ==0) {

        [scrollView setContentOffset:resultPoint animated:NO];

    }else{

        [scrollView setContentOffset:resultPoint animated:YES];

    }


    

}









- (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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值