用UIScroView实现图片的滚动

主要分为两步:1. 创建UIScrollView 2.创建UIPageControl。最后别忘了实现UIScrollViewDelegate

- (void)setupScrollView

{

    //创建UIScrollView

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

    scorllView.frame = self.view.bounds;

    [self.view addSubview:scorllView];

    scorllView.delegate = self;

    

    //添加图片

    CGFloat width = scorllView.width;

    CGFloat height = scorllView.height;

    for (int i = 0; i < 4; ++i)

    {

        // 拼接图片名称

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

        UIImage *image = [UIImage imageWithName:name];

        // 创建UIImageView

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

        iv.image = image;

        

        // 设置frame

        iv.width = width;

        iv.height = height;

        iv.y = 0;

        iv.x = i * iv.width;

        

        // 添加UIImageViewscrollerView

        [scorllView addSubview:iv];

    }

    // 设置UISrollView的其他属性

    // 设置contentsize

    scorllView.contentSize = CGSizeMake(WZNewfeatureImageCount * width, 0);

    // 设置分页

    scorllView.pagingEnabled = YES;

    // 隐藏指示条

    scorllView.showsHorizontalScrollIndicator = NO;

    // 设置没有弹簧效果

    scorllView.bounces = NO;

}


- (void)setupPageControl

{

    //创建UIPageControl

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

    control.numberOfPages = WZNewfeatureImageCount;

    //设置UIPageControlframe

    control.centerX = self.view.width * 0.5;

    control.centerY = self.view.height - 30;

    //设置当前页的颜色

    control.currentPageIndicatorTintColor = [UIColor redColor];

    //设置其他也的颜色

    control.pageIndicatorTintColor = [UIColor greenColor];

    //添加UIPageControl到控制器的View

    [self.view addSubview:control];

    

    self.control = control;

}


#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    // 1.计算页码

    int page = scrollView.contentOffset.x / self.view.width;

    // 2.设置页码

    self.control.currentPage = page;

}


PS:模拟器上没 home键,怎么返回的 

shift+cmd+h 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值