objective-c实现轮播图(17-08-09)

屏幕快照 2017-08-09 下午8.39.01.png
屏幕快照 2017-08-09 下午8.43.35.png

注:轮播图是7张,但要给8张图片,并且第一张和最后一张是同一张图片

//
//  ViewController.m
//  轮播图
//
//  Created by lanou3g on 17/8/9.
//  Copyright © 2017年 lanou3g. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () <UIScrollViewDelegate>

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

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.scrollView.delegate = self;
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width*7, self.view.frame.size.height);
    [self.view addSubview:self.scrollView];
    
    for (int i = 1; i <= 8; i++) {
        NSString *imageName1 = [NSString stringWithFormat:@"h%d.jpeg",i];
        CGFloat x = (i-1)*self.view.frame.size.width;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, self.view.frame.size.width, self.view.frame.size.height)];
        imageView.image = [UIImage imageNamed:imageName1];
        [self.scrollView addSubview:imageView];
    }
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 25, self.view.frame.size.width-200, 30)];
    self.pageControl.numberOfPages = 7;
    self.pageControl.currentPage = 0;
    [self.pageControl addTarget:self action:@selector(pageControlAction) forControlEvents:UIControlEventEditingChanged];
    [self.view addSubview:self.pageControl];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    [self.timer fire];
    
}
- (void)timerAction{
    CGFloat offsetX = self.scrollView.contentOffset.x;
    CGFloat offsetY = self.scrollView.contentOffset.y;
    CGFloat width = self.view.frame.size.width;
    [self.scrollView setContentOffset:CGPointMake(offsetX+width, offsetY) animated:YES];
    if (self.scrollView.contentOffset.x >= width * 7) {
        CGPoint point = CGPointMake(0, 0);
        self.scrollView.contentOffset = point;
    }
}

- (void)pageControlAction{
    CGFloat index = self.pageControl.currentPage;
    CGPoint point = CGPointMake(index*self.view.frame.size.width, 0);
    [self.scrollView setContentOffset:point animated:YES];
    
}


- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    [self.timer invalidate];
    self.timer = nil;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    [self.timer fire];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat x = scrollView.contentOffset.x;
    CGFloat width = self.view.frame.size.width;
    if (x>=width*7) {
        self.pageControl.currentPage = 0;
    }else {
        self.pageControl.currentPage = x/width;
    }
}

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

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,703评论 25 708
  • 主要思路 1.我们需要自定义一个继承自FrameLayout的布局,利用FrameLayout布-局的特性(在同一...
    ZebraWei阅读 2,358评论 0 5
  • “山间有闲云野鹤 卧龙古琴小酌 凭阴阳保乾坤没有想过要陷害哪个”
    予卿27阅读 203评论 1 1
  • 从明天起,做一个幸福的人 喂马,劈柴,周游世界 从明天起,关心粮食和蔬菜 我有一所房子,面朝大海,春暖花开 从明天...
    指鹿为彘阅读 248评论 0 1
  • 自己对自己不能太过松懈,一但松懈,那么你做任何事都将失去激情,愿你成为那个永远追逐梦想,永不轻言放弃的那个女孩,相...
    小菜籽5257阅读 85评论 0 0