美文网首页
objective-c实现轮播图(17-08-09)

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

作者: Hilarylii | 来源:发表于2017-08-09 20:45 被阅读0次
屏幕快照 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

相关文章

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

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

  • 轮播图心得

    轮播图 写轮播图之前我们要认识到几个问题:一、什么是轮播图?二、怎么实现轮播效果?三、轮播图还有什么小功能可以实现...

  • 封装轮播图(17-08-09)

    无限循环自动图片轮播器(一步设置即可使用)(github: https://github.com/gsdios/S...

  • 轮播图实现

    轮播图在实际应用开发使用较多,本文说明一下具体的实现过程。 一、实现轮播图的基本控件介绍。实现轮播图需要将多张图片...

  • swift UICollectionView 实现无限轮播图

    无线轮播图的实现方式有很多,这里介绍如何通过 UICollectionView 实现无线轮播图.效果图如下: 具体...

  • swift轮播图的实现-UIScrollView

    目标 :UIScrollView+三UIImageView的轮播图实现 原理:利用UIScrollView实现轮播...

  • Objective-C实现循环轮播图

    前几天实现轮播图,调试也费了点时间,顺便做个记录,方便以后使用。 使用pod管理代码,podfile里引入如下库:...

  • 项目-轮播图

    整个轮播图分为三部分:轮播指标、轮播项目及轮播导航。用boostrap实现轮播图要比用js、jQuery方便的多,...

  • YJBannerView 的使用方法 (iOS 11 适配)

    使用简单、功能丰富的 Objective-C版 轮播控件, 基于 UICollectionView 实现, 多种场...

  • 传统&呼吸 轮播

    传统的轮播图 一个 carousel 轮播图,图片实现自动轮播,可以左右按钮播放,点小圆点也能实现换图。同时设置节...

网友评论

      本文标题:objective-c实现轮播图(17-08-09)

      本文链接:https://www.haomeiwen.com/subject/uvnqrxtx.html