//
// LoopView.h
// Test_LoopScrollView
//
// Created by lanouhn on 14-8-30.
// Copyright (c) 2014年 vaercly@163.com 陈聪雷. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface LoopView : UIView
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIPageControl *pageControl;
@end
//
// LoopView.m
// Test_LoopScrollView
//
// Created by lanouhn on 14-8-30.
// Copyright (c) 2014年 vaercly@163.com 陈聪雷. All rights reserved.
//
#import "LoopView.h"
@implementation LoopView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setupScrollView];
[self setupPageControl];
}
return self;
}
- (void)setupScrollView
{
self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_scrollView.tag = 200;
_scrollView.contentSize = CGSizeMake(320 * 7, [UIScreen mainScreen].bounds.size.height);
// [_scrollView scrollRectToVisible:CGRectMake(320,0,320,[UIScreen mainScreen].bounds.size.height) animated:NO];
[_scrollView setContentOffset:CGPointMake(320, 0) animated:NO];
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.pagingEnabled = YES;
[self addSubview:_scrollView];
[_scrollView release];
for (int i = 0; i < 7; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(320 * i, 0, 320, [UIScreen mainScreen].bounds.size.height)];
if (i == 0) {
imageView.image = [UIImage imageNamed:@"girl_5.jpg"];
} else if (i == 6) {
imageView.image = [UIImage imageNamed:@"girl_1.jpg"];
} else {
imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"girl_%d", i]ofType:@"jpg"]];
}
[_scrollView addSubview:imageView];
[imageView release];
}
}
- (void)setupPageControl
{
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(10, [UIScreen mainScreen].bounds.size.height - 40, 300, 20)];
_pageControl.tag = 100;
_pageControl.numberOfPages = 5;
_pageControl.currentPage = 0;
_pageControl.pageIndicatorTintColor = [UIColor grayColor];
_pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];
[self addSubview:_pageControl];
[_pageControl release];
}
- (void)dealloc
{
self.scrollView = nil;
self.pageControl = nil;
[super dealloc];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
//
// RootViewController.m
// Test_LoopScrollView
//
// Created by lanouhn on 14-8-30.
// Copyright (c) 2014年 vaercly@163.com 陈聪雷. All rights reserved.
//
#import "RootViewController.h"
#import "LoopView.h"
@interface RootViewController ()<UIScrollViewDelegate>
{
BOOL _isOrder;
}
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
_isOrder = YES;
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changePage) userInfo:nil repeats:YES];
}
return self;
}
- (void)loadView
{
LoopView *loopView = [[LoopView alloc] initWithFrame:[UIScreen mainScreen].bounds];
loopView.backgroundColor = [UIColor grayColor];
self.view = loopView;
[loopView release];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
LoopView *loopView = ( LoopView *)self.view;
loopView.scrollView.delegate = self;
[loopView.pageControl addTarget:self action:@selector(handlePageControl:) forControlEvents:UIControlEventValueChanged];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:100];
int currentPage = scrollView.contentOffset.x / 320;
if (currentPage == 0) {
// [scrollView scrollRectToVisible:CGRectMake(320 * 5, 0, 320, [UIScreen mainScreen].bounds.size.height) animated:NO];
[scrollView setContentOffset:CGPointMake(320 * 5, 0) animated:NO];
pageControl.currentPage = 4;
} else if (currentPage == 6) {
// [scrollView scrollRectToVisible:CGRectMake(320, 0, 320, [UIScreen mainScreen].bounds.size.height) animated:NO];
[scrollView setContentOffset:CGPointMake(320, 0) animated:NO];
pageControl.currentPage = 0;
} else {
pageControl.currentPage = currentPage - 1;
}
}
- (void)handlePageControl:(UIPageControl *)pageControl
{
NSLog(@"%d", pageControl.currentPage);
UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:200];
// [scrollView scrollRectToVisible:CGRectMake(320 * (pageControl.currentPage + 1),0,320,[UIScreen mainScreen].bounds.size.height) animated:NO];
[scrollView setContentOffset:CGPointMake(320 * (pageControl.currentPage + 1), 0) animated:YES];
}
- (void)changePage
{
//单序版
/*
LoopView *loopView = ( LoopView *)self.view;
int page = loopView.pageControl.currentPage;
page++;
page = page > 4 ? 0 : page;
loopView.pageControl.currentPage = page;
// [loopView.scrollView scrollRectToVisible:CGRectMake(320 * (page + 1), 0, 320, [UIScreen mainScreen].bounds.size.height) animated:NO];
if (page) {
[loopView.scrollView setContentOffset:CGPointMake(320 * (page + 1), 0) animated:YES];
} else {
[loopView.scrollView setContentOffset:CGPointMake(320 * (page + 1), 0) animated:NO];
}
*/
//双序版
LoopView *loopView = ( LoopView *)self.view;
int page = loopView.pageControl.currentPage;
if (_isOrder) {
page++;
page = page > 4 ? 0 : page;
if (!page) {
_isOrder = NO;
page = 3;
}
} else {
page--;
page = page < 0 ? 0 : page;
if (!page) {
_isOrder = YES;
page = 0;
}
}
loopView.pageControl.currentPage = page;
[loopView.scrollView setContentOffset:CGPointMake(320 * (page + 1), 0) animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
滚动视图(UIScrollView)无限无缝左右切换图片,自动顺序加逆序循环播放图片
最新推荐文章于 2023-01-17 13:49:19 发布