原文链接:http://blog.csdn.net/xiaoxiangzhu660810/article/details/8728267
2013-06-13 10:35
148 查看
研究了一整天的成果,也借鉴了网上的一些资料,参考了http://huluwa.me/blog/ios-uiscrollview-loop/.的思想.主要实现了以下功能:
1.自动滑动图片,UIPageControl会随着图片滑动而改变;
2.用户可以手动切换图片,左右切换均可,当用户手动滑动图片时,图片停止自动切换;
3.实现了图片的循环播放;
4.UIPageControl可点击从而实现向左或右滑动,当用户点击UIPageControl时,图片不再自动切换;
直接上代码吧,不懂的后面还有工程和说明文档也一并附上(原来这个不能直接上传附件,要的就去资源页下载吧:http://download.csdn.net/detail/zhuiyi316/4434616)。
首先是.h文件,注意自己将IBOutlet和IBAction与storyBoard中拖入的控件相关联,我用的XCode4.3.1.
[plain] view
plaincopy
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIScrollViewDelegate>
{
NSMutableArray *imageArray;//存放图片
NSTimer *myTimer;//定时器
}
@property(nonatomic,retain) IBOutlet UIScrollView *myScrollView;
@property(nonatomic,retain) IBOutlet
UIPageControl *pageControl;
-(IBAction)pageTurn:(UIPageControl *)sender;
@end
然后是.m文件,自己添加图片。#import <QuartzCore/QuartzCore.h>是用来设置边角的。
[plain] view
plaincopy
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
@end
@implementation ViewController
@synthesize myScrollView,pageControl;
-(void)initArray
{
imageArray=[NSArray arrayWithObjects: [UIImage imageNamed:@"share.png"],[UIImage imageNamed:@"home_baike.png"],[UIImage imageNamed:@"home_chat.png"],[UIImage imageNamed:@"Icon.png"],nil];
//存放图片的数组
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self initArray];
[self configScrollView];
}
-(void)configScrollView
{
/*
@//初始化UIScrollView,设置相关属性,均可在storyBoard中设置
CGRect frame=CGRectMake(0, 0, 320, 480);
self.myScrollView = [[UIScrollView alloc]initWithFrame:frame]; //scrollView的大小
self.myScrollView.backgroundColor=[UIColor blueColor];
self.myScrollView.pagingEnabled=YES;//以页为单位滑动,即自动到下一页的开始边界
self.myScrollView.showsVerticalScrollIndicator=NO;
self.myScrollView.showsHorizontalScrollIndicator=NO;//隐藏垂直和水平显示条
*/
self.myScrollView.delegate=self;
UIImageView *firstView=[[UIImageView alloc] initWithImage:[imageArray lastObject]];
CGFloat Width=self.myScrollView.frame.size.width;
CGFloat Height=self.myScrollView.frame.size.height;
firstView.frame=CGRectMake(0, 0, Width, Height);
[self.myScrollView addSubview:firstView];
//set the last as the first
for (int i=0; i<[imageArray count]; i++) {
UIImageView *subViews=[[UIImageView alloc] initWithImage:[imageArray objectAtIndex:i]];
subViews.frame=CGRectMake(Width*(i+1), 0, Width, Height);
[self.myScrollView addSubview: subViews];
}
UIImageView *lastView=[[UIImageView alloc] initWithImage:[imageArray objectAtIndex:0]];
lastView.frame=CGRectMake(Width*(imageArray.count+1), 0, Width, Height);
[self.myScrollView addSubview:lastView];
//set the first as the last
[self.myScrollView setContentSize:CGSizeMake(Width*(imageArray.count+2), Height)];
[self.view addSubview:self.myScrollView];
[self.myScrollView scrollRectToVisible:CGRectMake(Width, 0, Width, Height) animated:NO];
//show the real first image,not the first in the scrollView
/*
@//设置pageControl的位置,及相关属性,可选
CGRect pageControlFrame=CGRectMake(100, 160, 78, 36);
self.pageControl=[[UIPageControl alloc]initWithFrame:pageControlFrame];
[self.pageControl setBounds:CGRectMake(0, 0, 16*(self.pageControl.numberOfPages-1), 16)];//设置pageControl中点的间距为16
[self.pageControl.layer setCornerRadius:8];//设置圆角
*/
self.pageControl.numberOfPages=imageArray.count;
// self.pageControl.backgroundColor=[UIColor blueColor];//背景
self.pageControl.currentPage=0;
self.pageControl.enabled=YES;
[self.view addSubview:self.pageControl];
[self.pageControl addTarget:self action:@selector(pageTurn:)forControlEvents:UIControlEventValueChanged];
myTimer=[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(scrollToNextPage:) userInfo:nil repeats:YES];
}
#pragma UIScrollView delegate
-(void)scrollToNextPage:(id)sender
{
int pageNum=self.pageControl.currentPage;
CGSize viewSize=self.myScrollView.frame.size;
CGRect rect=CGRectMake((pageNum+2)*viewSize.width, 0, viewSize.width, viewSize.height);
[self.myScrollView scrollRectToVisible:rect animated:NO];
pageNum++;
if (pageNum==imageArray.count) {
CGRect newRect=CGRectMake(viewSize.width, 0, viewSize.width, viewSize.height);
[self.myScrollView scrollRectToVisible:newRect animated:NO];
}
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat pageWidth=self.myScrollView.frame.size.width;
int currentPage=floor((self.myScrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;
if (currentPage==0) {
self.pageControl.currentPage=imageArray.count-1;
}else if(currentPage==imageArray.count+1){
self.pageControl.currentPage=0;
}
self.pageControl.currentPage=currentPage-1;
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[myTimer invalidate];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
myTimer=[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(scrollToNextPage:) userInfo:nil repeats:YES];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat pageWidth=self.myScrollView.frame.size.width;
CGFloat pageHeigth=self.myScrollView.frame.size.height;
int currentPage=floor((self.myScrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;
NSLog(@"the current offset==%f",self.myScrollView.contentOffset.x);
NSLog(@"the current page==%d",currentPage);
if (currentPage==0) {
[self.myScrollView scrollRectToVisible:CGRectMake(pageWidth*imageArray.count, 0, pageWidth, pageHeigth) animated:NO];
self.pageControl.currentPage=imageArray.count-1;
NSLog(@"pageControl currentPage==%d",self.pageControl.currentPage);
NSLog(@"the last image");
return;
}else if(currentPage==[imageArray count]+1){
[self.myScrollView scrollRectToVisible:CGRectMake(pageWidth, 0, pageWidth, pageHeigth) animated:NO];
self.pageControl.currentPage=0;
NSLog(@"pageControl currentPage==%d",self.pageControl.currentPage);
NSLog(@"the first image");
return;
}
self.pageControl.currentPage=currentPage-1;
NSLog(@"pageControl currentPage==%d",self.pageControl.currentPage);
}
-(IBAction)pageTurn:(UIPageControl *)sender
{
int pageNum=pageControl.currentPage;
CGSize viewSize=self.myScrollView.frame.size;
[self.myScrollView setContentOffset:CGPointMake((pageNum+1)*viewSize.width, 0)];
NSLog(@"myscrollView.contentOffSet.x==%f",myScrollView.contentOffset.x);
NSLog(@"pageControl currentPage==%d",self.pageControl.currentPage); [myTimer invalidate];
}
@end
1.自动滑动图片,UIPageControl会随着图片滑动而改变;
2.用户可以手动切换图片,左右切换均可,当用户手动滑动图片时,图片停止自动切换;
3.实现了图片的循环播放;
4.UIPageControl可点击从而实现向左或右滑动,当用户点击UIPageControl时,图片不再自动切换;
直接上代码吧,不懂的后面还有工程和说明文档也一并附上(原来这个不能直接上传附件,要的就去资源页下载吧:http://download.csdn.net/detail/zhuiyi316/4434616)。
首先是.h文件,注意自己将IBOutlet和IBAction与storyBoard中拖入的控件相关联,我用的XCode4.3.1.
[plain] view
plaincopy
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIScrollViewDelegate>
{
NSMutableArray *imageArray;//存放图片
NSTimer *myTimer;//定时器
}
@property(nonatomic,retain) IBOutlet UIScrollView *myScrollView;
@property(nonatomic,retain) IBOutlet
UIPageControl *pageControl;
-(IBAction)pageTurn:(UIPageControl *)sender;
@end
然后是.m文件,自己添加图片。#import <QuartzCore/QuartzCore.h>是用来设置边角的。
[plain] view
plaincopy
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
@end
@implementation ViewController
@synthesize myScrollView,pageControl;
-(void)initArray
{
imageArray=[NSArray arrayWithObjects: [UIImage imageNamed:@"share.png"],[UIImage imageNamed:@"home_baike.png"],[UIImage imageNamed:@"home_chat.png"],[UIImage imageNamed:@"Icon.png"],nil];
//存放图片的数组
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self initArray];
[self configScrollView];
}
-(void)configScrollView
{
/*
@//初始化UIScrollView,设置相关属性,均可在storyBoard中设置
CGRect frame=CGRectMake(0, 0, 320, 480);
self.myScrollView = [[UIScrollView alloc]initWithFrame:frame]; //scrollView的大小
self.myScrollView.backgroundColor=[UIColor blueColor];
self.myScrollView.pagingEnabled=YES;//以页为单位滑动,即自动到下一页的开始边界
self.myScrollView.showsVerticalScrollIndicator=NO;
self.myScrollView.showsHorizontalScrollIndicator=NO;//隐藏垂直和水平显示条
*/
self.myScrollView.delegate=self;
UIImageView *firstView=[[UIImageView alloc] initWithImage:[imageArray lastObject]];
CGFloat Width=self.myScrollView.frame.size.width;
CGFloat Height=self.myScrollView.frame.size.height;
firstView.frame=CGRectMake(0, 0, Width, Height);
[self.myScrollView addSubview:firstView];
//set the last as the first
for (int i=0; i<[imageArray count]; i++) {
UIImageView *subViews=[[UIImageView alloc] initWithImage:[imageArray objectAtIndex:i]];
subViews.frame=CGRectMake(Width*(i+1), 0, Width, Height);
[self.myScrollView addSubview: subViews];
}
UIImageView *lastView=[[UIImageView alloc] initWithImage:[imageArray objectAtIndex:0]];
lastView.frame=CGRectMake(Width*(imageArray.count+1), 0, Width, Height);
[self.myScrollView addSubview:lastView];
//set the first as the last
[self.myScrollView setContentSize:CGSizeMake(Width*(imageArray.count+2), Height)];
[self.view addSubview:self.myScrollView];
[self.myScrollView scrollRectToVisible:CGRectMake(Width, 0, Width, Height) animated:NO];
//show the real first image,not the first in the scrollView
/*
@//设置pageControl的位置,及相关属性,可选
CGRect pageControlFrame=CGRectMake(100, 160, 78, 36);
self.pageControl=[[UIPageControl alloc]initWithFrame:pageControlFrame];
[self.pageControl setBounds:CGRectMake(0, 0, 16*(self.pageControl.numberOfPages-1), 16)];//设置pageControl中点的间距为16
[self.pageControl.layer setCornerRadius:8];//设置圆角
*/
self.pageControl.numberOfPages=imageArray.count;
// self.pageControl.backgroundColor=[UIColor blueColor];//背景
self.pageControl.currentPage=0;
self.pageControl.enabled=YES;
[self.view addSubview:self.pageControl];
[self.pageControl addTarget:self action:@selector(pageTurn:)forControlEvents:UIControlEventValueChanged];
myTimer=[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(scrollToNextPage:) userInfo:nil repeats:YES];
}
#pragma UIScrollView delegate
-(void)scrollToNextPage:(id)sender
{
int pageNum=self.pageControl.currentPage;
CGSize viewSize=self.myScrollView.frame.size;
CGRect rect=CGRectMake((pageNum+2)*viewSize.width, 0, viewSize.width, viewSize.height);
[self.myScrollView scrollRectToVisible:rect animated:NO];
pageNum++;
if (pageNum==imageArray.count) {
CGRect newRect=CGRectMake(viewSize.width, 0, viewSize.width, viewSize.height);
[self.myScrollView scrollRectToVisible:newRect animated:NO];
}
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat pageWidth=self.myScrollView.frame.size.width;
int currentPage=floor((self.myScrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;
if (currentPage==0) {
self.pageControl.currentPage=imageArray.count-1;
}else if(currentPage==imageArray.count+1){
self.pageControl.currentPage=0;
}
self.pageControl.currentPage=currentPage-1;
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[myTimer invalidate];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
myTimer=[NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(scrollToNextPage:) userInfo:nil repeats:YES];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat pageWidth=self.myScrollView.frame.size.width;
CGFloat pageHeigth=self.myScrollView.frame.size.height;
int currentPage=floor((self.myScrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;
NSLog(@"the current offset==%f",self.myScrollView.contentOffset.x);
NSLog(@"the current page==%d",currentPage);
if (currentPage==0) {
[self.myScrollView scrollRectToVisible:CGRectMake(pageWidth*imageArray.count, 0, pageWidth, pageHeigth) animated:NO];
self.pageControl.currentPage=imageArray.count-1;
NSLog(@"pageControl currentPage==%d",self.pageControl.currentPage);
NSLog(@"the last image");
return;
}else if(currentPage==[imageArray count]+1){
[self.myScrollView scrollRectToVisible:CGRectMake(pageWidth, 0, pageWidth, pageHeigth) animated:NO];
self.pageControl.currentPage=0;
NSLog(@"pageControl currentPage==%d",self.pageControl.currentPage);
NSLog(@"the first image");
return;
}
self.pageControl.currentPage=currentPage-1;
NSLog(@"pageControl currentPage==%d",self.pageControl.currentPage);
}
-(IBAction)pageTurn:(UIPageControl *)sender
{
int pageNum=pageControl.currentPage;
CGSize viewSize=self.myScrollView.frame.size;
[self.myScrollView setContentOffset:CGPointMake((pageNum+1)*viewSize.width, 0)];
NSLog(@"myscrollView.contentOffSet.x==%f",myScrollView.contentOffset.x);
NSLog(@"pageControl currentPage==%d",self.pageControl.currentPage); [myTimer invalidate];
}
@end
相关文章推荐
- Linux下/dev/mem和/dev/kmem的区别 本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux
- Ubuntu 10.04地址导航栏修改为显示路径 本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/20
- Windows让我们养成了什么臭毛病 本篇文章来源于 黑基网-中国最大的网络安全站点 原文链接:http://www.hackbase.com/news/2010-05-10/35154.html
- 原文出自【比特网】,转载请保留原文链接:http://soft.chinabyte.com/database/493/12549493.shtml
- 选择Java接口还是抽象类 本篇文章来自IDC专家网 原文链接:http://www.soidc.net/articles/1213781061058/20001129/1214154133658_1.html
- 【转载】Android Studio jar、so、library项目依赖,原文链接http://zhengxiaopeng.com/2014/12/13/Android-Studio-jar、so、library项目依赖/
- 博科SAN交换机学习笔记之二:配置文件备份与固件升级 作者 LiaoJL | 转载时请务必以超链接形式标明文章原文链接和作者信息及本版权声明。 原文链接:http://www.liaojl.co
- 安装 MySQL与MySQL GUI Tools本篇文章来源于:开发学院 http://edu.codepub.com 原文链接:http://edu.codepub.com/2009/0724/10870.php
- 关于数据库中的无限级别设计的讨论 转自:泰州SEO[http://www.96SEO.com] 原文链接:http://www.96seo.com/
- Java并发编程:Callable、Future和FutureTask(原文链接:http://www.cnblogs.com/dolphin0520/p/3949310.html)
- Linux Android 多点触摸协议 原文出自【比特网】,转载请保留原文链接:http://soft.chinabyte.com/os/71/12306571.shtml
- Asp.net 火狐和IE的兼容性代码解析本篇文章来源于:开发学院 http://edu.codepub.com 原文链接:http://edu.codepub.com/2010/0308/20818.php
- 自动备份sql server2005数据库本篇文章来源于:开发学院 http://edu.codepub.com 原文链接:http://edu.codepub.com/2011/0323/30321.php
- Android开发:用getDrawingCache方法获取ImageView中的图像需要注意的问题 本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http:/
- 在 CentOS7 上安装 Zookeeper-3.4.9 服务 本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/L
- Linux Android 多点触摸协议 原文出自【比特网】,转载请保留原文链接:http://soft.chinabyte.com/os/71/12306571.shtml
- 移动时代的信息需求(原文链接http://blog.sina.com.cn/s/blog_593af2a70101ac1k.html)
- Windows XP硬盘安装Ubuntu 11.10双系统全程图解 本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com
- 两阶段提交协议 (原文链接http://blog.csdn.net/nieanan3602/article/details/8375077)
- 在U-boot下实现自动识别启动Flash的原理 本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/201