IOS 下划线随视图移动+自动轮播图功能

//
//  ContentView.h
//  test08 下划线随视图移动 简单版本实现
//
//  Created by mouweng on 17/8/27.
//  Copyright © 2017年 mouweng. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ContentView : UIScrollView

+(instancetype)shareWithFrame:(CGRect)frame;

@end


//
//  ContentView.m
//  test08 下划线随视图移动 简单版本实现
//
//  Created by mouweng on 17/8/27.
//  Copyright © 2017年 mouweng. All rights reserved.
//

#import "ContentView.h"
#define SCREEN_W            [[UIScreen mainScreen] bounds].size.width
#define SCREEN_H            [[UIScreen mainScreen] bounds].size.height

@implementation ContentView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        for(int i=0; i< 4 ;i++)
        {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_W*i, 5, SCREEN_W, SCREEN_H-5)];
            imageView.backgroundColor = [UIColor whiteColor];
            NSString *str = [NSString stringWithFormat:@"17_%d.jpg",i+12];
            imageView.image = [UIImage imageNamed:str];
            [self addSubview:imageView];
        }
    }
    return self;
}


+(instancetype)shareWithFrame:(CGRect)frame
{
    ContentView *contentView = [[self alloc]initWithFrame:frame];
    contentView.contentSize = CGSizeMake(SCREEN_W*4, frame.size.height);
    contentView.pagingEnabled = YES;
    contentView.bounces = NO;
    return contentView;
}


@end


//
//  headView.h
//  test08 下划线随视图移动 简单版本实现
//
//  Created by mouweng on 17/8/27.
//  Copyright © 2017年 mouweng. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface headView : UIView
+(instancetype)createHeadView:(CGRect)frame;
@property (strong,nonatomic)UIView *animationView;//创建下划线视图
@end

//
//  headView.m
//  test08 下划线随视图移动 简单版本实现
//
//  Created by mouweng on 17/8/27.
//  Copyright © 2017年 mouweng. All rights reserved.
//

#import "headView.h"
#import "ViewController.h"

#define padding 10
#define Width       [[UIScreen mainScreen] bounds].size.width
#define Height      [[UIScreen mainScreen] bounds].size.height

@implementation headView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (UIView *)animationView
{
    if(!_animationView)
    {
        _animationView = [[UIView alloc] initWithFrame:CGRectMake(0, 5,Width/4 , 5)];
        _animationView.backgroundColor = [UIColor redColor];
    }
    return _animationView;
}

//重写初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        //创建动画视图
        [self addSubview:self.animationView];
    }
    return self;
}

//类方法
+ (instancetype)createHeadView:(CGRect)frame
{
    headView *hV = [[self alloc] initWithFrame:frame];
    return hV;
}


//




//
//  LoopScrollView.h
//  拥有下划线随视图移动而移动和自动轮播的UIScrollView
//
//  Created by mouweng on 17/8/28.
//  Copyright © 2017年 mouweng. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "ContentView.h"
#import "headView.h"
@interface LoopScrollView : UIView<UIScrollViewDelegate>
//两个主视图元素控件
@property (strong,nonatomic)ContentView *contentView;
@property (strong,nonatomic)headView *headView;
//定式器以及页面控制器功能
@property (nonatomic, retain) NSTimer * timer;  //设置定时器
@property (nonatomic, retain) UIPageControl * pageControl;
@property (nonatomic, retain) UILabel * currentPageLabel;


- (void) startTimer;
- (void) stopTimer;

- (id)initWithFrame:(CGRect)frame;
@end

//
//  LoopScrollView.m
//  拥有下划线随视图移动而移动和自动轮播的UIScrollView
//
//  Created by mouweng on 17/8/28.
//  Copyright © 2017年 mouweng. All rights reserved.
//

#import "LoopScrollView.h"
#import "ContentView.h"
#import "headView.h"
#define SCREEN_WIDTH  self.frame.size.width
#define SCREEN_HEIGHT self.frame.size.height

@implementation LoopScrollView

static NSUInteger pageCount = 4;
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithFrame:(CGRect)frame
{
    self= [super initWithFrame:frame];
    if(self)
    {
        //创建头视图
        _headView = [headView createHeadView:CGRectMake(0, 0, SCREEN_WIDTH, 5)];
        [self addSubview:_headView];
        
        //创建内容视图
        _contentView = [ContentView shareWithFrame:CGRectMake(0, 5, SCREEN_WIDTH, SCREEN_HEIGHT-5)];
        _contentView.tag = -2;
        _contentView.delegate = self;
        [self addSubview:_contentView];
        
        //初始化定时器
        _timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(loopImageTimer:) userInfo:nil repeats:YES];
        
        //初始化UIPageControl
        int pageControlHeight = 30;
        CGFloat scrollViewBottom = _contentView.frame.origin.y + _contentView.frame.size.height;
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(10, scrollViewBottom-pageControlHeight, 100, pageControlHeight)];
        _pageControl.currentPage = 0;//当前页数所在的page位置
        _pageControl.numberOfPages = pageCount;
        _pageControl.backgroundColor = [UIColor grayColor];
        [self addSubview:_pageControl];
        
        //初始化标签 当前页/总页数
        _currentPageLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 50, _pageControl.frame.origin.y , 50, pageControlHeight)];
        _currentPageLabel.text = [NSString stringWithFormat:@"%d / %ld",1,pageCount];
        _currentPageLabel.textColor = [UIColor redColor];
        [self addSubview:_currentPageLabel];
        
    }
    
    return self;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

        NSInteger page = scrollView.contentOffset.x/SCREEN_WIDTH;
        [UIView animateWithDuration:0.2 animations:^{
            CGRect frame = self.headView.animationView.frame;
            frame = CGRectMake(SCREEN_WIDTH/4*page, frame.origin.y, frame.size.width, frame.size.height);
            self.headView.animationView.frame = frame;
        }];
    
}

//循环定时器设置
#pragma mark - Actions
- (void) loopImageTimer:(NSTimer *)timer {
    NSUInteger totalNum = pageCount;
    if (totalNum > 1) {
        CGPoint newOffset = _contentView.contentOffset;
        newOffset.x = newOffset.x + CGRectGetWidth(_contentView.frame);
        if (newOffset.x > (CGRectGetWidth(_contentView.frame) * (totalNum-1))) {
            newOffset.x = 0 ;
        }
        int index = newOffset.x / CGRectGetWidth(_contentView.frame);   //当前是第几个视图
        newOffset.x = index * CGRectGetWidth(_contentView.frame);
        
        _pageControl.currentPage = index;
        _currentPageLabel.text = [NSString stringWithFormat:@"%d / %ld",index+1, totalNum];
        [_contentView setContentOffset:newOffset animated:YES];
    }else{
        [_timer setFireDate:[NSDate distantFuture]];//关闭定时器
    }
}

//点击事件
- (void) loopImageOnClicked:(UITapGestureRecognizer *) tapGestureRecognizer{
    NSInteger imageViewTag = tapGestureRecognizer.view.tag;
    NSLog(@"imageViewTag: ----------------- %ld", imageViewTag);        // 0, 1, 2
    
}

//开始和停止
#pragma mark - Methods
- (void) startTimer {
    [_timer setFireDate:[NSDate distantPast]];
}

- (void) stopTimer {
    [_timer setFireDate:[NSDate distantFuture]];
}



//
//  ViewController.m
//  拥有下划线随视图移动而移动和自动轮播的UIScrollView
//
//  Created by mouweng on 17/8/28.
//  Copyright © 2017年 mouweng. All rights reserved.
//

#import "ViewController.h"
#import "LoopScrollView.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    LoopScrollView* loopScrollView = [[LoopScrollView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:loopScrollView];
    
    
}

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

@end




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本Demo使用UICollectionView实现自动无限轮播功能。 主要功能: 1.实现自动轮播,可修改轮播的时间 2.轮播图片可以来自本地,也可来自网络,通过单独的方法进行设置即可。对于加载网络图片时,Demo中使用了YYWebImage,也可自行替换成SDWebImage。 3.重写了和系统UIPageControl一样的功能,可用图片代替PageControl上的点点,也可自定义其颜色以及切换动画。 使用方法:使用方法比较简单。 /** * 加载本地图片Banner */ - (void)setupLocalBannerImageView { NSArray *array = @[@"1.png", @"2.png", @"3.png", @"4.png", @"5.png"]; FFBannerView *bannerVew = [FFBannerView bannerViewWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200) locationImageArray:array]; bannerVew.timeInterval = 2.0; [self.view addSubview:bannerVew]; } /** * 加载网络图片Banner */ - (void)setupNetWorkBannerImageView { NSArray *array = @[@"http://i3.download.fd.pchome.net/t_960x600/g1/M00/07/09/oYYBAFMv8q2IQHunACi90oB0OHIAABbUQAAXO4AKL3q706.jpg", @"http://images.weiphone.net/attachments/photo/Day_120308/118871_91f6133116504086ed1b82e0eb951.jpg", @"http://benyouhuifile.it168.com/forum/macos/attachments/month_1104/110425215921926a173e0f728e.jpg", @"http://benyouhuifile.it168.com/forum/macos/attachments/month_1104/1104241737046031b3a754f783.jpg"]; FFBannerView *bannerVew = [FFBannerView bannerViewWithFrame:CGRectMake(0, 250, [UIScreen mainScreen].bounds.size.width, 200) netWorkImageArray:array placeHolderImage:nil]; bannerVew.timeInterval = 2.0; bannerVew.pageControlStyle = FFPageControlStyleMiddle; bannerVew.delegate = self; [self.view addSubview:bannerVew]; } 以上方式即可简单使用,如需自定义PageControl也可继承FFAbstractDotView,做些基本的设置即可。 gitHub下载地址:喜欢的朋友请给个星呗! 欢迎各位一起来讨论,有问题请发邮箱[email protected]或者直接加我QQ:270452746进行讨论。谢谢!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值