假设做一个循环滚动UIScrollView
先上效果图:
首先初始化:
- (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的更多相关文章
- 假设做一个精美的Login界面(攻克了一EditText自带clear的功能,相似iphone的UITextField)
先上图: XML为: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...
- Application_Start事件中用Timer做一个循环任务
protected void Application_Start(object sender, EventArgs e) { System.Timers.Timer timer = new Syste ...
- APP中常见上下循环滚动通知的简单实现,点击可进入详情
APP中常见上下循环滚动通知的简单实现,点击可进入详情 关注finddreams博客,一起分享一起进步!http://blog.csdn.net/finddreams/article/details/ ...
- Jquery制作--循环滚动列表
自己模仿JQ插件的写法写了一个循环滚动列表插件,支持自定义上.下.左.右四个方向,支持平滑滚动或者间断滚动两种方式,都是通过参数设置.JQ里面有些重复的地方,暂时没想到更好的方法去精简.不过效果还是可 ...
- UIScrollView 循环滚动,代码超简单
如今非常多应用里面多多少少都用到了循环滚动,要么是图片.要么是view,或者是其它,我总结一下,写了个demo分享给大家. 先看代码之后在讲原理: 1.创建一个空的项目(这个我就不多说了). 2.加入 ...
- UIScrollView循环滚动1
现在基本每一个商业APP都会有循环滚动视图,放一些轮播广告之类的,都是放在UIScrollView之上.假如我要实现N张图片的轮播,我借鉴了几个博文,得到两种方法实现: [第一种]:如下图(图片来源于 ...
- 使用UIScrollView和UIPageControl做一个能够用手势来切换图片的效果
利用UIScrollView的滚动效果来实现,先上图: 实现过程是:在viewController里先增加UIScrollView和UIPageControl: -(void) loadView { ...
- 使用UIScrollView 结合 UIImageView 实现图片循环滚动
场景: 在开发工作中,有时我们需要实现一组图片循环滚动的情况.当我们使用 UIScrollView 结合 UIImageView 来实现时,一般 UIImageView 会尽量考虑重用,下面例子是以( ...
- UIScrollView实现自动循环滚动广告
实现效果如下: 功能说明: 程序运行,图片自动循环播放,采用定时器实现; 当用户用手势触摸滑动时,定时器的自动播放取消,停止触摸时,自动无限播放; 代码如下 : 采用封装视图,外部进行调用即可: 1. ...
随机推荐
- js进阶 12-5 jquery中表单事件如何使用
js进阶 12-5 jquery中表单事件如何使用 一.总结 一句话总结:表单事件如何使用:可元素添加事件监听,然后监听元素,和javase里面一样. 1.表单获取焦点和失去焦点事件有哪两组? 注意是 ...
- python2.7一步步实现键盘和鼠标检测
由于现在python3的pyHook库还没有很完善: 所以选择了python2.7版本 关于环境的搭建 python2.7 用的是anaconda,直接去官网下载去安装就好用,不需要配置环境变量的, ...
- Setup iOS Development Environment.
Setup iOS Development Environment Install XCode and check-out source code from SVN XCode Please find ...
- ds finder 唤醒
http://www.hangge.com/blog/cache/detail_594.html
- [HTTP] Understand 2xx HTTP Status Code Responses
The 2xx family of status codes are used in HTTP responses to indicate success. Beyond the generic 20 ...
- [CSS] Design for Mobile First with Tachyons
Tachyons provides extensions (-ns, -m, and -l) to many of its classes to help you design for respons ...
- Java算法--串的简单处理
题目例如以下: 串的处理 在实际的开发工作中.对字符串的处理是最常见的编程任务. 本题目即是要求程序对用户输入的串进行处理.详细规则例如以下: 1. 把每个单词的首字母变为大写. 2. 把数字与字母之 ...
- ios开发网络学习五:MiMEType ,多线程下载文件思路,文件的压缩和解压缩
一:MiMEType:一般可以再百度上搜索到相应文件的MiMEType,或是利用c语言的api去获取文件的MiMEType : //对该文件发送一个异步请求,拿到文件的MIMEType - (void ...
- MSYS2是对MSYS的一个独立的重写,是基于当前的Cygwin和MinGW-w64重写的,以同原生的Windows软件有更好的交互性为目的
MSYS2的官网:http://sourceforge.net/projects/msys2/ 官网的描述: Description MSYS2 is an independent rewrite o ...
- php 获取根目录
在网站根目录的index.php文件里 define('BASE_PATH',str_replace('\\','/',realpath(dirname(__FILE__).'/'))."/ ...