先上效果图:

首先初始化:

- (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. IPv4与IPv6数据报格式详解

    摘要: 本文给出IPv4与IPv6数据报格式示意图,并整理了各个字段含义,最后对比IPv4与IPv6数据报格式的区别. 一.IPv4数据报 图1 IPv4数据报格式版本号(version) 不同的IP ...

  2. 最正经的php post get

    https://www.cnblogs.com/ps-blog/p/6732448.html /** * 模拟post进行url请求 * @param string $url * @param str ...

  3. GAS Syntax

    GAS or GNU as syntax is a different form of syntax for assembly language files, known also as AT& ...

  4. swift学习:自定义Log

    import UIKit /* 总结:1:let file = (#file as NSString).lastPathComponent,#file获取的是打印所在的文件 的全路径,转成NSStri ...

  5. 用python的库监听鼠标程序测试,有程序,有现象

    程序如下: # -*- coding: utf-8 -*- import pythoncom, pyHook  def OnMouseEvent(event):   print 'MessageNam ...

  6. 【u123】最大子段和

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 给出一段序列,选出其中连续且非空的一段使得这段和最大. [输入格式] 输入文件maxsum1.in的第 ...

  7. jquery-11 jquery中的事件切换如何实现

    jquery-11 jquery中的事件切换如何实现 一.总结 一句话总结:事件切换hover()和toggle()函数.参数两个,都是函数,依次执行两个函数. 1.如何实现单击切换图片? 用togg ...

  8. 配置java 环境变量(jdk)

    java环境变量需要配置3个: JAVA_HOME:D:\Program Files (x86)\Java\jdk1.8 CLASSPATH:.;%JAVA_HOME%\lib\dt.jar;%JAV ...

  9. 在Excel中粘贴时怎样跳过隐藏行

    http://www.excel123.cn/Article/exceljichu/201203/932.html 有时在筛选后需要将其他区域中的连续行数据复制粘贴到筛选区域,以替换筛选后的数据.由于 ...

  10. Windows Phone 8.1 FilePicker API

    在 Windows Phone 8.1 中,增加了 FilePicker 的方式与文件打交道,最大的亮点在于这种方式不仅可以浏览手机上的文件,还可以浏览符合协议的应用里的文件! 比如点击 OneDri ...