YIem`s Blog -心比天高命比纸薄-链接找不到的请在站内搜索内容!

UI8_轮播图-图片无限滚动iOS


// RootViewController.m
// **UI8_轮播图
//**
// Created by YIem on 15/12/16.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "RootViewController.h"

@interface RootViewController ()<UIScrollViewDelegate>

@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIPageControl *page;
@property (nonatomic, retain) NSTimer *timer;

@end

@implementation RootViewController



// 图片数组
NSMutableArray *photoArr = [NSMutableArray array];
for (NSInteger i = 0; i < 4; i++) {
    //图片名
    NSString *name = [NSString stringWithFormat:@"%ld", i];
    // 图片路径
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"jpg"];
    /* 当前图片在外面 用路径
     当前图片在里面 不能也路径
     直接  UIImage *img = [UIIMage
     
     */
    
    // image
    UIImage *img = [UIImage imageWithContentsOfFile:path];
    //添加数组
    [photoArr addObject:img];
}
// 无限循环 最后添加第一页
[photoArr addObject:photoArr.firstObject];

// 轮播图
self.scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
_scrollView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:_scrollView];
[_scrollView release];
// 内容大小
_scrollView.contentSize = CGSizeMake(photoArr.count* 375, 0);
_scrollView.pagingEnabled = YES;
// 小图片跟随图片
_scrollView.delegate = self;
// 铺图片
for (NSInteger i = 0; i < photoArr.count; i++) {
    // imageView
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(i * 375, 0, 375, 667)];
    imgView.image = photoArr[i];
    [_scrollView addSubview:imgView];
    [imgView release];
}

// pageControl
self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, 150, 30)];
_page.center = CGPointMake(self.view.center.x, 600);
_page.numberOfPages = 4;
_page.pageIndicatorTintColor = [UIColor lightGrayColor];
_page.currentPageIndicatorTintColor = [UIColor blackColor];
[self.view addSubview:_page];
[_page release];
// 点击事件
[_page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];

// 自适应边界
self.automaticallyAdjustsScrollViewInsets = NO;


// timer 自动滚
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(roll) userInfo:nil repeats:YES];

}

// 自动滚
/*

*/

// 拖拽时 停止timer

// 两秒钟后 执行方法

// 图片跟随点变化

// [_scrollView setContentOffset:CGPointMake(page.currentPage * 375, 0) animated:YES];

[_scrollView setContentOffset:CGPointMake(page.currentPage * 375, 0) animated:YES];

}
// 点跟随图片变化

/*

pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

*/

@end

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »