先上效果图:

首先初始化:

- (void)viewDidLoad
{
//加入最后一张图 用于循环
int length = 4;
NSMutableArray *tempArray = [NSMutableArray array];
for (int i = 0 ; i < length; i++)
{ NSString* str = [NSString stringWithFormat:@"title%d",i];
[tempArray addObject:str];
} NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:length+2]; if (length > 1)
{
NSString *str1 = [tempArray objectAtIndex:length-1]; //title3
SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithTitle:str1 tag:-1];
[itemArray addObject:item];
} for (int i = 0; i < length; i++)
{
NSString *str2 = [tempArray objectAtIndex:i];
SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithTitle:str2 tag:i];
[itemArray addObject:item]; } //加入第一张图 用于循环
if (length >1)
{
NSString *str3 = [tempArray objectAtIndex:0];
SGFocusImageItem *item = [[SGFocusImageItem alloc] initWithTitle:str3 tag:length]; //title0
[itemArray addObject:item];
}
ViewFrame *bannerView = [[ViewFrame alloc] initWithFrame:CGRectMake(0, 0, 320, 105) imageItems:itemArray isAuto:NO]; [self.view addSubview:bannerView]; }
SGFocusImageItem仅仅有一个Title和tag:
@interface SGFocusImageItem : NSObject

@property (nonatomic, strong)  NSString     *title;
@property (nonatomic, assign) NSInteger tag; - (id)initWithTitle:(NSString *)title tag:(NSInteger)tag; @end

主要是ViewFrame:

#define ITEM_WIDTH 320.0
@interface ViewFrame : UIView<UIScrollViewDelegate> @property(nonatomic,strong)NSMutableArray* imageItems;
@property(nonatomic,strong)UIScrollView* scrollView; - (id)initWithFrame:(CGRect)frame imageItems:(NSArray *)items isAuto:(BOOL)isAuto;
@end
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
float targetX = _scrollView.contentOffset.x;
NSLog(@"=====scrollViewdidscroll targetX=%f",targetX); <strong> if ([_imageItems count] >= 3) {
//假设超过最后一张图,则又一次设置setContentOffset
if (targetX >= ITEM_WIDTH * ([_imageItems count]-1)) {
targetX = ITEM_WIDTH;
[_scrollView setContentOffset:CGPointMake(targetX, 0) animated:NO];
}
else if(targetX <= 0){
targetX = ITEM_WIDTH * ([_imageItems count] - 2);
[_scrollView setContentOffset:CGPointMake(targetX, 0) animated:NO];
}
}</strong>
} - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
} -(void)setupViews{
_scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
_scrollView.scrollsToTop = NO; CGSize size = CGSizeMake(320, 0);
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
_scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width * _imageItems.count,
_scrollView.frame.size.height);
[self addSubview:_scrollView];
for (int i = 0; i < _imageItems.count; i++) {
SGFocusImageItem* item = [_imageItems objectAtIndex:i];
int x = i * _scrollView.frame.size.width;
int width = _scrollView.frame.size.width;
int height = _scrollView.frame.size.height; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, width, height)];
if (i == 0) {
imageView.backgroundColor = [UIColor brownColor];
} else if(i == 1) {
imageView.backgroundColor = [UIColor orangeColor];
} else if(i == 2) {
imageView.backgroundColor = [UIColor blueColor];
}else if(i == 3){
imageView.backgroundColor = [UIColor redColor];
}else if(i == 4){
imageView.backgroundColor = [UIColor greenColor];
}else if(i == 5) {
imageView.backgroundColor = [UIColor lightGrayColor];
} UITextField* field = [[UITextField alloc] initWithFrame:CGRectMake(x+160, 30, 50, 50)];
field.text = item.title;
// NSLog(@"====kkkkk title=%@",item.title);
field.textAlignment = UITextAlignmentCenter;
field.font = [UIFont systemFontOfSize:18]; [_scrollView addSubview:imageView];
[_scrollView addSubview:field]; } } - (id)initWithFrame:(CGRect)frame imageItems:(NSArray *)items isAuto:(BOOL)isAuto{
self = [super initWithFrame:frame];
if (self) {
_imageItems = [NSMutableArray arrayWithArray:items];
[self setupViews]; }
return self;
}

代码能够在http://download.csdn.net/detail/baidu_nod/7679089下载

版权声明:本文博主原创文章,博客,未经同意不得转载。

假设做一个循环滚动UIScrollView的更多相关文章

  1. 假设做一个精美的Login界面(攻克了一EditText自带clear的功能,相似iphone的UITextField)

    先上图:     XML为: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...

  2. Application_Start事件中用Timer做一个循环任务

    protected void Application_Start(object sender, EventArgs e) { System.Timers.Timer timer = new Syste ...

  3. APP中常见上下循环滚动通知的简单实现,点击可进入详情

    APP中常见上下循环滚动通知的简单实现,点击可进入详情 关注finddreams博客,一起分享一起进步!http://blog.csdn.net/finddreams/article/details/ ...

  4. Jquery制作--循环滚动列表

    自己模仿JQ插件的写法写了一个循环滚动列表插件,支持自定义上.下.左.右四个方向,支持平滑滚动或者间断滚动两种方式,都是通过参数设置.JQ里面有些重复的地方,暂时没想到更好的方法去精简.不过效果还是可 ...

  5. UIScrollView 循环滚动,代码超简单

    如今非常多应用里面多多少少都用到了循环滚动,要么是图片.要么是view,或者是其它,我总结一下,写了个demo分享给大家. 先看代码之后在讲原理: 1.创建一个空的项目(这个我就不多说了). 2.加入 ...

  6. UIScrollView循环滚动1

    现在基本每一个商业APP都会有循环滚动视图,放一些轮播广告之类的,都是放在UIScrollView之上.假如我要实现N张图片的轮播,我借鉴了几个博文,得到两种方法实现: [第一种]:如下图(图片来源于 ...

  7. 使用UIScrollView和UIPageControl做一个能够用手势来切换图片的效果

    利用UIScrollView的滚动效果来实现,先上图: 实现过程是:在viewController里先增加UIScrollView和UIPageControl: -(void) loadView { ...

  8. 使用UIScrollView 结合 UIImageView 实现图片循环滚动

    场景: 在开发工作中,有时我们需要实现一组图片循环滚动的情况.当我们使用 UIScrollView 结合 UIImageView 来实现时,一般 UIImageView 会尽量考虑重用,下面例子是以( ...

  9. UIScrollView实现自动循环滚动广告

    实现效果如下: 功能说明: 程序运行,图片自动循环播放,采用定时器实现; 当用户用手势触摸滑动时,定时器的自动播放取消,停止触摸时,自动无限播放; 代码如下 : 采用封装视图,外部进行调用即可: 1. ...

随机推荐

  1. windows+python3+opencv3.4安装

    1.安装anaconda. 2.pip install opencv-python 网上很多关于python opencv安装说明,步骤极其繁琐,其实按照本说明只需两步就可安装完成.

  2. [HTML] Change an HTML5 input's placeholder color with CSS

    We will look at what CSS selectors to use to change an HTML5 inputs placeholder color. This can diff ...

  3. imresize() 函数——matlab

    功能:改变图像的大小. 用法:B = imresize(A,m)B = imresize(A,m,method)B = imresize(A,[mrows ncols],method) B = imr ...

  4. Emacs常用快捷键

    基本命令 C-x C-f 打开/新建文件 C-x C-s 保存当前缓冲区 C-x C-w 当前缓冲区另存为 C-x C-v 关闭当前Buffer并打开新文件 C-x i 光标处插入文件 C-x b 切 ...

  5. spark安装与调试

    I---- 1---jdk and scala install ****zyp@ubuntu:~/Desktop/software$ tar xvf jdk-7u67-linux-i586.tar.g ...

  6. (二)SSO之CAS框架单点退出,自己定义退出界面.

    用CAS的退出,仅仅能使用它自己的那个退出界面,假设有这种要求, 要求退出后自己主动跳转到登录界面, 该怎样做呢? 以下这篇文章实现了退出后能够自己定义跳转界面. 用了CAS,发现退出真是个麻烦事,退 ...

  7. NSCache使用常见错误

    NSCache用来存储缓存数据的时候.和NSDictionary功能类似, 可是NSCache有一个特别的问题: 一旦接收到内存警告之后,假设使用[NSCache removeAllObjects]处 ...

  8. poj2151之概率DP

    Check the difficulty of problems Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4403   ...

  9. 适合前端开发的 Chrome 扩展有哪些?(十款)

    适合前端开发的 Chrome 扩展有哪些?(十款) 一.总结 好的插件或者框架对程序员的意义重大. 二.适合前端开发的 Chrome 扩展有哪些?(十款) 掘金是一个高质量的技术社区,从 ECMASc ...

  10. mysqlsla slow-query常用用法

    mysqlsla -lt slow /data/mysql/testdb2-slow.log -sf -top 20 -sort t_sum > /data/mysql/my_testdb2-s ...